通过python requests / bs提交表单

问题描述 投票:0回答:1

我在网页上有以下表格:

<form method="POST" action="" name=“x”>Saison (click): 
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2011" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2012" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2013" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2014" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2015" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2016" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2017" type="submit">
    <input style="border:1px dotted silver;background-color:white" name=“y” value="2018" type="submit">
</form>

我想使用BeautifulSoup并请求获取当我按下其中一个提交按钮时生成的网页。这是如何运作的?

python python-3.x forms beautifulsoup request
1个回答
0
投票

在“网络”选项卡的开发人员工具中,您可以看到表单数据仅包含jama_saison:2013。因此,要传递此数据,您必须使用此:

year = 2013
data = {'jama_saison': year}
r = requests.post('https://www.oejv.com/bundesliga/tabellestandings/', data=data)

要测试我们是否得到了正确的答案:

soup = BeautifulSoup(r.text, 'lxml')
print(soup.find('th', class_='bl-name').text)
# Erste Judo-Bundesliga 2013

只需将year变量更改为您想要的年份。将其改为2017年,将Erste Judo-Bundesliga 2017作为输出。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.