简体   繁体   English

如何使用Python请求发送表单数据

[英]How to Send Form Data with Python Requests

I'm using requests to access this webpage and subsequently parsing and inspecting the HTML with Beautiful Soup. 我正在使用访问此网页的请求,随后使用Beautiful Soup解析和检查HTML。

This page allows a user to specify the number of days in the past for which results should be returned. 该页面允许用户指定过去应该返回结果的天数。 This is accomplished via a form on the page: 这是通过页面上的表单完成的:

天数选择

When I submit the request in the browser with my selection of 365 days and examine the response, I find this form data was sent with the request: 当我选择了365天在浏览器中提交请求并检查响应时,发现此表单数据是随请求发送的:

索取表格数据

Of note is the form datum "dnf_class_values[procurement_notice][_posted_date]: 365" as this is the only element that corresponds with my selection of 365 days. 值得注意的是基准格式“ dnf_class_values [procurement_notice] [_ posted_date]:365”,因为这是与我选择的365天相对应的唯一元素。

When this request is returned in the browser, I get n results, where n is the maximum number possible given this is the largest time period possible. 在浏览器中返回此请求时,我得到n个结果,其中n是最大可能数,因为这是最大可能的时间段。 n is visible in the markup as <span class="lst-cnt"> . n在标记中显示为<span class="lst-cnt">

I can't seem to duplicate the sending of that form data with requests. 我似乎无法与请求重复发送该表单数据。 Here is the relevant portion of my code: 这是我的代码的相关部分:

import requests
from bs4 import BeautifulSoup as bs

formData = {'dnf_class_values[procurement_notice][_posted_date]':'365'}
r = requests.post("https://www.fbo.gov/index?s=opportunity&mode=list&tab=list&tabmode=list&pp=20&pageID=1", data = formData)
s = bs(r.content)
s.find('span',{'class':'lst-cnt'})

This is returning the same number of results as when the form is submitted with the default value for number of days. 这将返回与提交表单时相同的结果数,并使用默认值(天数)提交表单。

I've tried URL encoding the key in data , as well as using requests.get , and specifying params as opposed to data . 我已经尝试过URL编码data的密钥,以及使用requests.get并指定与data相对的params Additionally, I've attempted to append the form data field as a query string parameter: 此外,我尝试将表单数据字段附加为查询字符串参数:

url...?s=opportunity&mode=list&tab=list&tabmode=list&pp=20&pageID=1&dnf_class_values%5Bprocurement_notice%5D%5B_posted_date%5D=365

What is the appropriate syntax for that request? 该请求的适当语法是什么?

You cannot send only the sections you care about, you need to send everything . 您不能仅发送您关心的部分,而是需要发送所有内容 Duplicate the POST request that Chrome made exactly. 复制与Chrome完全相同的POST请求。

Note that some of the POSTed values may be CSRF tokens. 请注意,某些POSTED值可能是CSRF令牌。 The Base64-encoded strings are particularly likely ( dnf_opt_template , dnf_opt_template_dir , dnf_opt_subform_template and dnf_class_values[procurement_notice][notice_id] ), and should probably be pulled out of the HTML for the original page using BeautifulSoup. Base64编码的字符串特别有可能( dnf_opt_templatednf_opt_template_dirdnf_opt_subform_templatednf_class_values[procurement_notice][notice_id] ),并且应该使用BeautifulSoup从原始页面的HTML中提取出来。 The rest can be hardcoded. 其余的可以硬编码。

Otherwise, your original syntax was correct. 否则,您的原始语法正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM