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

Tim*_*tty 1 python beautifulsoup web-scraping python-requests

我试图从这个站点获取商店列表:http : //www.health.state.mn.us/divs/cfh/wic/wicstores/

我想获取当您单击“查看所有商店”按钮时生成的商店列表。我知道我可以使用 Selenium 或 MechanicalSoup 或...来做到这一点,但我希望使用请求。

看起来点击按钮提交了一个表单:

 <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" />
Run Code Online (Sandbox Code Playgroud)

但我不知道如何编写请求查询(或者甚至可能的话)。

到目前为止,我尝试的原因是以下方面的变化:

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

但这不起作用。欢迎任何帮助/建议。

SIM*_*SIM 5

如果您考虑选择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)
Run Code Online (Sandbox Code Playgroud)

部分输出:

1st Quality Market
33rd Meat & Grocery
52 Market  And Trading
75 Market And Deli
7th Grocery
9th Ave X-Press
Run Code Online (Sandbox Code Playgroud)