如何从BeautifulSoup获得搜索结果?

2024-09-24 06:33:14 发布

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

我还不太习惯于美化群体(尽管它非常有用)。我的问题是,如果我有这样一个网站

https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp

我将P2RY12输入到“基因名”输入框得到结果,我需要做什么?你知道吗

另外,一般来说,如果我想从某个网站获得搜索结果,我需要做什么?你知道吗


Tags: httpsmodulessearch网站基因pagescnac
1条回答
网友
1楼 · 发布于 2024-09-24 06:33:14

如果你打开Firefox/Chrome网站管理员工具,你可以观察到页面在哪里发出请求。因此,当在搜索框中键入P2RY12并单击submit按钮时,页面正在向http://bigd.big.ac.cn/dogsdv2/indsnp/searchIndSNPSingle.action发出POST请求。你知道吗

一般来说,您需要知道URL和发送到URL的参数才能获得任何信息。你知道吗

此示例从结果的第一页获取一些信息:

import requests
from bs4 import BeautifulSoup

url = 'http://bigd.big.ac.cn/dogsdv2/indsnp/searchIndSNPSingle.action'

data = {
    'totalCount': -1,
    'searchForm.chrom': 0,
    'searchForm.start': '',
    'searchForm.end': '',
    'searchForm.rsid': '',
    'searchForm.popu':  0,
    'searchForm.geneid': '',
    'searchForm.genename': 'P2RY12',
    'searchForm.goterm': '',
    'searchForm.gokeyword': '',
    'searchForm.limitFlag': 1,
    'searchForm.numlimit':  1000
}

headers = {
    'Referer': 'https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp',
}

soup = BeautifulSoup(requests.post(url, data=data, headers=headers).text, 'html.parser')

for td in soup.select('table.table7 tr > td:nth-child(3)'):
    a = td.select_one('a')
    print('SNP ID:', a.get_text(strip=True))
    t1 = a.find_next_sibling('br').find_next_sibling(text=True)
    print('Position:', t1.strip())
    print('Location:', ', '.join( l.get_text(strip=True) for l in t1.find_next_siblings('a') ))
    print('Genotype:', a.find_next_siblings('br')[2].find_next_sibling(text=True).strip())
    print('-' * 80)

印刷品:

SNP ID: cfa19627795
Position: Chr23:45904511
Location: ENSCAFG00000008485, ENSCAFG00000008531, ENSCAFG00000008534
Genotype: G
                                        
SNP ID: cfa19627797
Position: Chr23:45904579
Location: ENSCAFG00000008485, ENSCAFG00000008531, ENSCAFG00000008534
Genotype: C
                                        
SNP ID: cfa19627803
Position: Chr23:45904842
Location: ENSCAFG00000008485, ENSCAFG00000008531, ENSCAFG00000008534
Genotype: C
                                        

...and so on.

相关问题 更多 >