【发布时间】:2018-12-28 17:12:00
【问题描述】:
我正在尝试使用beautifulsoup 编写一个网络爬虫来从https://www.ncbi.nlm.nih.gov/gene/?term=Celiac+disease 中提取基因名称
我的代码可以从第一页得到我想要的结果,但我不知道如何编写代码让我的程序移动到下一页。单击“下一步”按钮后,我得到一个与上一页无关的新地址。 例如,第一页的地址是https://www.ncbi.nlm.nih.gov/gene/?term=Celiac+disease,但下一页的地址是https://www.ncbi.nlm.nih.gov/gene(但仍然显示与乳糜泻相关的结果)
我查了google和stackoverflow,看看有没有与这个问题相关的文章。但我只能找到有关具有相似地址的页面的文章,并且(对我而言)合乎逻辑地追随他们的踪迹。
from bs4 import BeautifulSoup
from urllib.request import urlopen
gene_result = []
url = "https://www.ncbi.nlm.nih.gov/gene/?term=Celiac+disease"
html = urlopen(url).read()
soup = BeautifulSoup(html, "html5lib")
tbody = soup.find("tbody")
a_href = tbody.find_all("a")
for x in a_href:
gene = x.contents[0]
gene_result.append(gene)
print(gene_result)
代码在第一页爬得很好,我得到了很好的结果:
['CTLA4', 'HLA-DQA1', 'IL2', 'IL21', 'CCR3', 'CELIAC2', 'ATXN2', 'SH2B3', 'HLA-DQB1', 'CELIAC5', 'TAGAP', 'CELIAC7', 'CELIAC13', 'CELIAC12', 'CELIAC11', 'CELIAC10', 'CELIAC9', 'CELIAC8', 'CELIAC6', 'KIAA1109']
有人可以帮我解决这个问题吗?
【问题讨论】:
-
您是否考虑过使用 selenium 之类的东西?
标签: python web-scraping beautifulsoup web-crawler