我是网络抓取和网络事务的新手(但几乎习惯于Python),我想了解如何将网站搜索整合到生物信息学研究工具中。< / p>
目标:在http://www.lovd.nl/3.0/search
上检索表单的输出import mechanicalsoup
# Connect to LOVD
browser = mechanicalsoup.StatefulBrowser()
browser.open("http://www.lovd.nl/3.0/search")
# Fill-in the search form
browser.select_form('#websitevariantsearch')
browser["variant"] = "chr15:g.40699840C>T"
browser.submit_selected()
# Display the results
print(browser.get_current_page())
在输出中,我得到了完全相同的页面(http://www.lovd.nl/3.0/search)。我尝试了标准请求,但我得到了另一种错误:
from requests import get, Session
url="http://www.lovd.nl/3.0/search"
formurl = "http://www.lovd.nl/3.0/ajax/search_variant.php"
client = Session()
#get the csrf
soup = BeautifulSoup(client.get(url).text, "html.parser")
csrf = soup.select('form input[name="csrf_token"]')[0]['value']
form_data = {
"search": "",
"csrf_token": csrf,
"build": "hg19",
"variant": "chr15:g.40699840C>T"
}
response = get(formurl, data=form_data)
html=response.content
return html
...这只返回
alert("Error while sending data.");
form_data字段来自XHR请求(来自开发人员 - &gt;网络标签)。
我可以看到数据是通过ajax异步发送的,但我不明白这些信息的实际含义。
需要一些指导
答案 0 :(得分:1)
MechanicalSoup不执行JavaScript。您尝试浏览的网站有:
<form id="websitevariantsearch"
action=""
onsubmit="if ...">
传统的HTML表单没有任何操作,但是在提交时会执行一段JavaScript。 MechanicalSoup在这里没有帮助。 Selenium可能有效:http://mechanicalsoup.readthedocs.io/en/stable/faq.html#how-does-mechanicalsoup-compare-to-the-alternatives