Can I use requests.post to submit a form?
我想从这个网站获取商店列表:
http://www.health.state.mn.us/divs/cfh/wic/wicstores/
我想获得点击"查看所有商店"按钮时生成的商店列表。 我知道我可以使用Selenium或MechanicalSoup或...来做到这一点,但我希望使用请求。
看起来点击按钮会提交一个表单:
1 2 | <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" /> |
但我不知道如何编写请求查询(或者甚至可能这样做)。
为什么我到目前为止尝试的变化是:
1 2 3 | SITE = 'http://www.health.state.mn.us/divs/cfh/wic/wicstores/' data = {'name': 'setAllStores', 'form': 'submitAllStores', 'input': 'submitAllStores'} r = requests.post(SITE, data) |
但这不起作用。 欢迎任何帮助/建议。
如果您考虑选择
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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) |
部分输出:
1 2 3 4 5 6 | 1st Quality Market 33rd Meat & Grocery 52 Market And Trading 75 Market And Deli 7th Grocery 9th Ave X-Press |