带有 mechanicalsoup 的表单请求未显示预期结果

Form request with mechanicalsoup not showing expected results

一般来说,我是网络抓取和网络事物的新手(但已经习惯了 Python),我想了解如何将网站搜索集成到生物信息学中研究工具。

目标:检索 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

...而这个 returns 只有一个

alert("Error while sending data.");

form_data 字段取自 XHR 请求(来自开发人员 -> 网络选项卡)。

我可以看到数据是通过 ajax 异步发送的,但我不明白这些信息的实际含义。

需要一些指导

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