使用靓汤导航到下一页

2024-10-03 11:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用beautiful soup浏览结果的所有页面。例如,我必须从这个网站上删除:

http://www.ncbi.nlm.nih.gov/pubmed

搜索查询是
“((肿瘤学)和乳腺癌”,结果是“
,没有引号。
我怎么把所有的书页拿来?我尝试在请求头中查找表单数据。尝试修改一些字段。我可以修改它,每一页有200个条目。但不会再有了。实际上,我需要遍历页面来获取所有内容。 任何帮助都将不胜感激。在

假设现在,我只想看看第四页。在

规范相关部分:

post_params = {
    'term' : val,
         'EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Pubmed_DisplayBar.PageSize' : 20,
'EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Pubmed_DisplayBar.sPageSize' : 20,
'coll_start' : 61,
'citman_count' : 20,
'citman_start' : 61,
'coll_start2' : 61,
'citman_count2' : 20,
'citman_start2' : 61,
'CollectionStartIndex': 1,
'CitationManagerStartIndex' : 1,
'CitationManagerCustomRange' : 'false',

'EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Entrez_Pager.cPage' : 3,
'EntrezSystem2.PEntrez.PubMed.Pubmed_ResultsPanel.Entrez_Pager.CurrPage' : 4,

}

"""This part handles the scraping business"""
post_args = urllib.urlencode(post_params)
baseurl = 'http://www.ncbi.nlm.nih.gov'
url = 'http://www.ncbi.nlm.nih.gov/pubmed/'
page = urllib2.urlopen(url, post_args)
page = page.read()
soup = BeautifulSoup(page)
soup.prettify()

它仍然获取第一页。一旦这一部分成功,我就在考虑迭代这个代码,每次修改参数。在


Tags: httpwwwpagencbinlmpostgovsoup
1条回答
网友
1楼 · 发布于 2024-10-03 11:26:41

永远不要抓取PubMed总是有一个更简单的方法直接检索数据。安装并使用BioPython包。下面是一个简单的脚本,可以使用您的查询获取前10篇论文:

from Bio import Entrez, Medline

# Always tell NCBI who you are  
Entrez.email = "your_address@example.com"  

term="((oncology) AND breast cancer) AND resulted in"

handle = Entrez.esearch(db="pubmed", retmax=10, term=term)
record = Entrez.read(handle)

print record['Count']  # see how many hits in your search

for ref in record['IdList']:
    handle = Entrez.efetch(db="pubmed", id=ref, 
                           rettype="Medline", 
                           retmode="text")
    paper = Medline.read(handle)
    # Medline returns a dict from which we can extract the 
    # fields we desire
    print '-' * 30
    print paper['TI']
    print
    print paper['AB']

该手册内容广泛,但您只需阅读有关使用biopythonentrez搜索和获取记录以及使用biopythonmedline解析结果的部分。在

相关问题 更多 >