我想从这个网站获取商店列表: 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" />
但是我不知道如何编写请求查询(或者甚至可能这样做)。
为什么我到目前为止尝试的是:
SITE = 'http://www.health.state.mn.us/divs/cfh/wic/wicstores/'
data = {'name': 'setAllStores', 'form': 'submitAllStores', 'input': 'submitAllStores'}
r = requests.post(SITE, data)
但这不起作用。欢迎任何帮助/建议。
答案 0 :(得分:2)
如果您考虑选择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)
部分输出:
1st Quality Market
33rd Meat & Grocery
52 Market And Trading
75 Market And Deli
7th Grocery
9th Ave X-Press