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

编程入门 行业动态 更新时间:2024-10-07 15:23:42
本文介绍了我可以使用request.post提交表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to get the list of stores from this site: 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 = '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("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提交表单吗?

本文发布于:2023-11-02 07:03:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   表单   request   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!