简体   繁体   English

我可以使用request.post提交表单吗?

[英]Can I use requests.post to submit a form?

I am trying to get the list of stores from this site: http://www.health.state.mn.us/divs/cfh/wic/wicstores/ 我正在尝试从此站点获取商店列表: http : //www.health.state.mn.us/divs/cfh/wic/wicstores/

I'd like to get the list of stores that is produced when you click on the button "View All Stores". 我想获取单击“查看所有商店”按钮时产生的商店的列表。 I understand that I could use Selenium or MechanicalSoup or ... to do this but I was hoping to use requests. 我知道我可以使用Selenium或MechanicalSoup或...来执行此操作,但我希望使用请求。

It looks like clicking on the button submits a form: 看起来像单击按钮提交了一个表单:

 <form name="setAllStores" id="setAllStores" action="/divs/cfh/wic/wicstores/index.cfm" method="post" onsubmit="return _CF_checksetAllStores(this)">
<input name="submitAllStores" id="submitAllStores"  type="submit" value="View All Stores" />

But I have no idea how to write the requests query (or indeed if this is even possible). 但是我不知道如何编写请求查询(或者甚至是否有可能)。

Why I have tried so far is variations on: 到目前为止,我为什么要尝试以下变化:

SITE = 'http://www.health.state.mn.us/divs/cfh/wic/wicstores/'
data = {'name': 'setAllStores', 'form': 'submitAllStores', 'input': 'submitAllStores'}
r = requests.post(SITE, data)

But this doesn't work. 但这是行不通的。 Any help / advice would be welcome. 任何帮助/建议都将受到欢迎。

Try the below code to populate the result, if you considered to select view all stores option. 如果您考虑选择view all stores选项,请尝试使用以下代码填充结果。

import requests
from bs4 import BeautifulSoup

FormData={
    'submitAllStores':'View All Stores'
}
with requests.Session() as s:
    s.headers = {"User-Agent":"Mozilla/5.0"}
    res = s.post("http://www.health.state.mn.us/divs/cfh/wic/wicstores/index.cfm",data=FormData)
    soup = BeautifulSoup(res.text, 'lxml')
    for item in soup.select(".info"):
        shopname = item.select_one(".info-service").text
        print(shopname)

Partial output: 部分输出:

1st Quality Market
33rd Meat & Grocery
52 Market  And Trading
75 Market And Deli
7th Grocery
9th Ave X-Press

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM