我重新编辑了我的问题,以便更好地解释我的问题
我正试图从ORCID数据库(科学文章和作者数据库(中获取一个人的名字和姓氏。
我使用requests_html
和.render()
访问url:
"https://orcid.org/orcid-search/search?searchQuery=0000-0001-9077-1041";并从中获取html代码,对html进行解析并存储在_text
列表中。(如果你访问url,你会看到它包含ORCID数据库的搜索结果,id为"0000-0001-9077-1041"-名称:"Andreas"和姓氏:"Leimbach"以及一些额外的数据(。
我想从该页面的html代码中检索姓名和姓氏文本。然而,当我多次运行程序时,有时名称和姓氏会出现在输出结果中,有时则不然。我希望程序能够在所有方面检索相同的数据。
我使用以下Python脚本:
from requests_html import HTMLSession
from bs4 import BeautifulSoup
def GetCredentialsFromORCID(_id):
base_url = "https://orcid.org/orcid-search/search?searchQuery=" + _id
session = HTMLSession()
response = session.get(base_url)
response.html.render()
soup = BeautifulSoup(response.html.html, 'lxml')
_text = soup.get_text().strip().split()
print("This is whet we got:n", _text)
GetCredentialsFromORCID("0000-0001-9077-1041")
(试着运行此代码几次(5-10次以上(,然后自己看看(。
我只能假设这可能与这个页面使用JavaScript有关,因为我一直收到:
Please enable JavaScript to continue using this application.
在控制台上,但我对此了解不多。
有人能帮我吗?
网页实际上会在初始搜索之后运行扩展搜索。您可以重新编写代码,将扩展搜索用作初始调用,然后只需要请求。你当然可以重新做下面的例子。它的结构很简单,就像你最初接受id并返回响应一样。包含最少的错误处理。
def GetCredentialsFromORCID(_id):
import requests
r = requests.get(f'https://pub.orcid.org/v3.0/expanded-search/?start=0&rows=200&q=orcid:{_id}',
headers = {'User-Agent':'Mozilla/5.0', 'accept' : 'application/json'})
try:
return r.json()
except Exception as e:
return (f'Error for {_id}', e)
print(GetCredentialsFromORCID("0000-0001-9077-1041"))