基于Selenium的IEEE批量RIS引用下载

基于Selenium的IEEE批量RIS引用下载,第1张

近期为了写一份综述,需要将大量的文章引用插入到文章中去。由于平时看论文都是用onenote做文献管理,对写作时添加参考文献没有一点儿用处。
在文章中添加参考文献的思路笔者只知道两种,一种是使用latex写作,添加参考文献时使用BibTex格式。其二是使用word写作,然后在word中添加参考文献。
由于此次笔者使用的是word写作,添加参考文献就显得格外困难,恰好,EndNote在word中的插件可以很方便的添加参考文献。另一方面,由于参考文献大部分来源于IEEE,而向EndNote导入IEEE文献一般使用RIS格式。
具体可参考这个链接,不再多说。
综上,可以得到笔者的需求
1.在IEEE上下载已知标题的论文的RIS引用
2. 将下载的RIS格式添加到EndNote中
笔者有以下的条件:
1.所有需要引用的参考文献pdf文件均已经存放在同一个文件夹中,且pdf文件名为论文标题。
2.已经在EndNote 中打开了一个Library。
3.默认使用EndNote打开RIS文件

注意:由于各种虽然知道原因但不知道解决方法的问题,以及各种不知道原因的问题,下载结果并不一定是所需的,使用时还需要自行检验

python版本:3.6.8
chrome版本:101.0.4951.41
运行环境:vscode 1.66.2
EndNote版本:EndNote X9(Bld 12062)

下面就是使用python实现需求的代码

from selenium import webdriver  

import os
import time
import re

# 一些基础 *** 作的参考
# selenium基础教程 https://zhuanlan.zhihu.com/p/111859925
# chromedriver下载地址 https://chromedriver.storage.googleapis.com/index.html
# 兄弟节点定位和父节点定位 https://blog.csdn.net/xuemeilu/article/details/124484986
# 通过系统默认方式打开文件 https://www.cnpython.com/qa/73652
# 字符串中是否包含汉字 https://blog.csdn.net/shouwangcc/article/details/48157643
# 参考了一些其他人的工作
# https://blog.csdn.net/qq_37189298/article/details/117394233
# https://blog.csdn.net/weixin_50097774/article/details/121564394
def downloadRIS(filename,flag):
    try:
        flag = flag +1
        searchBox =  driver.find_element_by_xpath('//input[@aria-label="Enter search text"]')
        searchButton = driver.find_element_by_xpath('//div[@]')
        searchBox.click()
        searchBox.send_keys(filename.split(".")[0])
        searchButton.click()
        time.sleep(2)       # 由于等候时间较短,当搜索结果较多时会报错,从而可以手动寻找需要的文献,同时也有可能因为延迟稍大而报错
        checkBox = driver.find_element_by_xpath('//input[@aria-label="Select search result"]')
        checkBox.click()
        exportIcon = driver.find_element_by_xpath('//i[@]')
        exportIcon = driver.find_element_by_xpath("//a[contains(text(),'Export')]/child::i")
        exportIcon.click()
        chooseCitation = driver.find_element_by_xpath("//a[contains(text(),'Citation')]").click()
        RISlocate = driver.find_element_by_xpath('//label[@for="download-ris"]/child::input')
        RISlocate.click()
        citationType = driver.find_element_by_xpath('//label[@for="citation-abstract"]/child::input')
        if flag == 0:
            citationType.click()
        exportButton = driver.find_element_by_xpath("//button[contains(text(),'Export')]")
        driver.execute_script("arguments[0].click();", exportButton)
    except:  # 对于各种原因都会出现报错,懒得根据情况一一用程序解决了,直接手动下载吧
        print(filename)
        os.system("pause")


def  not_have_hz(contents):  
    Pattern = re.compile(u'[\u4e00-\u9fa5]+')    
    match = Pattern.search(contents)
    return not match 



path = r"C:\Users\usename\Desktop\"	#存储论文pdf文件的文件夹的地址,注意最后的两个反斜杠

names = os.listdir(path)

url = "https://ieeexplore.ieee.org/Xplore/home.jsp"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(2)

flag = 0
for filename in names:
    if not_have_hz(filename):
        downloadRIS(filename,flag)

# 由于各种虽然知道原因但不知道解决方法的问题,以及各种不知道原因的问题,下载结果并不一定是所需的,使用时还需要自行检验
driver.quit()
print("引用文件下载完成")
# 浏览器默认下载地址,对于笔者的电脑,RIS格式的默认打开方式为endnote,故直接打开RIS文件即可实现参考文献导入
downloadpath = r'C:\Users\usename\Downloads\'      # 浏览器默认下载地址
downloadList = os.listdir(downloadpath)
for downloadName in downloadList:
    try:
        os.startfile(downloadpath + downloadName)
        time.sleep(3)
    except:
        print(downloadpath + downloadName)
        os.system("pause")
print("引用添加完成")
print("Missions Complete")

以上是笔者的一些浅见,代码中也可能存在一些漏洞和缺陷,还请大佬们在评论区中或私聊中不吝指出。
如果您对文章中的内容存在疑惑或者需要一些帮助,欢迎评论或私聊。

文章已经同步发布于微信公众号:别玩了来学习。欢迎关注

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/868197.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存