我可以使用request.post提交表单吗? [英] Can I use requests.post to submit a form?

查看:474
本文介绍了我可以使用request.post提交表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此站点获取商店列表: http://www.health.state.mn.us/divs/cfh/wic/wicstores/

I am trying to get the list of stores from this site: http://www.health.state.mn.us/divs/cfh/wic/wicstores/

我想获取单击查看所有商店"按钮时产生的商店的列表.我知道我可以使用Selenium或MechanicalSoup或...来执行此操作,但我希望使用请求.

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.

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

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.

推荐答案

如果您考虑选择view all stores选项,请尝试使用以下代码填充结果.

Try the below code to populate the result, if you considered to select view all stores option.

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

这篇关于我可以使用request.post提交表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆