PubMed 单篇文献基本信息获取

https://pubmed.ncbi.nlm.nih.gov/33883728/

PubMed 多篇文献基本信息获取

文章对应链接的获取

在搜索页中,默认为十篇,先爬取一篇文章的链接

复制 Xpath ://*[@id="search-results"]/section/div[2]/div/article[1]/div[2]/div[1]/a

根据验证的跳转文章链接为:[https://pubmed.ncbi.nlm.nih.gov/33883728/](https://pubmed.ncbi.nlm.nih.gov/33883728/),可以确定,Xpath 中的href属性是我们需要的,

那么获取链接的代码:

python 1 行
1
link = html.xpath('//*[@id="search-results"]/section/div[2]/div/article[1]/div[2]/div[1]/a/@href')

接下来就要想如何利用循环将这一页的 10 篇文章的链接获取到?

1、2、3 篇文章对应的 Xpath:

python 3 行
1
2
3
//*[@id="search-results"]/section/div[2]/div/article[1]/div[2]/div[1]/a
//*[@id="search-results"]/section/div[2]/div/article[2]/div[2]/div[1]/a
//*[@id="search-results"]/section/div[2]/div/article[3]/div[2]/div[1]/a

可以发现,发生变化的是 article[]

python 4 行
1
2
3
4
for num in range(1,11):
    link = html.xpath(f'//*[@id="search-results"]/section/div[2]/div/article[{num}]/div[2]/div[1]/a/@href')
    link = 'https://pubmed.ncbi.nlm.nih.gov' + link[0]
    print(link)

完整代码:

针对遇到的一些问题,进行了修正:

翻页获取多篇文献信息

记录链接:

python 3 行
1
2
3
https://pubmed.ncbi.nlm.nih.gov/?term=Atherosclerosis
https://pubmed.ncbi.nlm.nih.gov/?term=Atherosclerosis&page=2
https://pubmed.ncbi.nlm.nih.gov/?term=Atherosclerosis&page=3

并且[https://pubmed.ncbi.nlm.nih.gov/?term=Atherosclerosis&page=1](https://pubmed.ncbi.nlm.nih.gov/?term=Atherosclerosis&page=1)可以打开第一页

python 4 行
1
2
3
4

for pageNum in range(1, 11):
    url = f"https://pubmed.ncbi.nlm.nih.gov/?term=Atherosclerosis&page={pageNum}"
    getLinks(url)

完整代码

使用 pandas 处理数据

https://pandas.pydata.org/