我正试图用漂亮的汤来编写一个网络爬虫,从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 (但仍然显示与腹腔疾病相关的结果)
我已经查找了谷歌和堆栈溢出,看看是否有任何与此问题相关的文章。但是,我只能找到一些关于有类似地址的页面的文章,(对我来说)符合逻辑地跟踪它们的轨迹。
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']有人能帮我解决这个问题吗?
发布于 2018-12-29 07:39:25
对于这样的网站,您需要使用名为使用python的selenium。的东西。
您需要模拟从您的python代码中单击"next“按钮,然后将html_source读入BeautifulSoup。
https://stackoverflow.com/questions/53962010
复制相似问题