使用 .net 网站 (python) 上的请求发布表单 [英] Post forms using requests on .net website (python)

查看:25
本文介绍了使用 .net 网站 (python) 上的请求发布表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import requests

headers ={
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"en-US,en;q=0.5",
"Connection":"keep-alive",
"Host":"mcfbd.com",
"Referer":"https://mcfbd.com/mcf/FrmView_PropertyTaxStatus.aspx",
"User-Agent":"Mozilla/5.0(Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"}

a = requests.session()
soup = BeautifulSoup(a.get("https://mcfbd.com/mcf/FrmView_PropertyTaxStatus.aspx").content)

payload = {"ctl00$ContentPlaceHolder1$txtSearchHouse":"",
"ctl00$ContentPlaceHolder1$txtSearchSector":"",
"ctl00$ContentPlaceHolder1$txtPropertyID":"",
"ctl00$ContentPlaceHolder1$txtownername":"",
"ctl00$ContentPlaceHolder1$ddlZone":"1",
"ctl00$ContentPlaceHolder1$ddlSector":"2",
"ctl00$ContentPlaceHolder1$ddlBlock":"2",
"ctl00$ContentPlaceHolder1$btnFind":"Search",
"__VIEWSTATE":soup.find('input',{'id':'__VIEWSTATE'})["value"],
"__VIEWSTATEGENERATOR":"14039419",
"__EVENTVALIDATION":soup.find("input",{"name":"__EVENTVALIDATION"})["value"],
"__SCROLLPOSITIONX":"0",
"__SCROLLPOSITIONY":"0"}

b = a.post("https://mcfbd.com/mcf/FrmView_PropertyTaxStatus.aspx",headers = headers,data = payload).text
print(b)

以上是我在这个网站上的代码.

above is my code for this website.

https://mcfbd.com/mcf/FrmView_PropertyTaxStatus.aspx

我检查了萤火虫,这些是表单数据的值.但是这样做:

I checked firebug out and these are the values of the form data. however doing this:

b = requests.post("https://mcfbd.com/mcf/FrmView_PropertyTaxStatus.aspx",headers = headers,data = payload).text
print(b)

抛出这个错误:

[ArgumentException]: Invalid postback or callback argument

我对通过请求提交表单的理解是否正确?

is my understanding of submitting forms via request correct?

1.打开萤火虫

2.提交表单

3.转到 NET 标签

4.在NET选项卡上选择帖子选项卡

5.像上面的代码一样复制表单数据

我一直想知道如何做到这一点.我可以使用硒,但我想我会尝试一些新的东西并使用请求

I've always wanted to know how to do this. I could use selenium but I thought I'd try something new and use requests

推荐答案

您收到的错误是正确的,因为像 _VIEWSTATE(以及其他)这样的字段不是静态的或硬编码的.正确的做法如下:

The error you are receiving is correct because the fields like _VIEWSTATE (and others as well) are not static or hardcoded. The proper way to do this is as follows:

创建一个请求会话对象.此外,建议使用包含 USER-AGENT 字符串的标题更新它 -

Create a Requests Session object. Also, it is advisable to update it with headers containing USER-AGENT string -

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36",}`
s = requests.session()

导航到指定的网址 -

Navigate to the specified url -

r = s.get(url)

使用BeautifulSoup4解析返回的html -

Use BeautifulSoup4 to parse the html returned -

from bs4 import BeautifulSoup
soup = BeautifulSoup(r.content, 'html5lib')

使用硬编码值和动态值填充 formdata -

Populate formdata with the hardcoded values and dynamic values -

formdata = {
   '__VIEWSTATE': soup.find('input', attrs={'name': '__VIEWSTATE'})['value'],
   'field1': 'value1'
}

然后使用会话对象本身发送 POST 请求 -

Then send the POST request using the session object itself -

s.post(url, data=formdata)

这篇关于使用 .net 网站 (python) 上的请求发布表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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