gpt4 book ai didi

python - 如何使用 python 将表单发布到 aspx 网站

转载 作者:太空宇宙 更新时间:2023-11-03 20:56:20 24 4
gpt4 key购买 nike

我正在尝试使用 python 在此网站上进行搜索查询 https://www.ahpra.gov.au/Registration/Registers-of-Practitioners.aspx?m=Search

但我不确定如何更正我的代码,如下所示:

from bs4 import BeautifulSoup
import requests

s = requests.session()
url="https://www.ahpra.gov.au/Registration/Registers-of-Practitioners.aspx?m=Search"
r = s.get(url)
soup = BeautifulSoup(r.content, 'html5lib')

formdata = {
'__VIEWSTATE': soup.find('input', attrs={'name': '__VIEWSTATE'})['value'],
"__EVENTTARGET": "",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"__VIEWSTATEFIELDCOUNT": "2",
"ctl22$ctl00$ddBoards": "",
"ctl22$ctl00$ucSearch$txtSearch": "",
"ctl22$ucSearch$txtSearch": "",
"ctl22$ddBoards": "",
"content_0$contentcolumnmain_0$txtFamilyName": "Mccarthy",
"content_0$contentcolumnmain_0$txtGivenName": "",
"content_0$contentcolumnmain_0$txtRegistrationNumber": "",
"content_0$contentcolumnmain_0$ddlProfession": "",
"content_0$contentcolumnmain_0$txtSuburb": "",
"content_0$contentcolumnmain_0$txtPostcode": "",
"content_0$contentcolumnmain_0$ddlState": "",
"content_0$contentcolumnmain_0$btnSearch": "Search"
}

headers={"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-AU,en-GB;q=0.9,en-US;q=0.8,en;q=0.7",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Content-Length": "9282",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "ASP.NET_SessionId=gj3sldhgi23iixqp513jrxx1; __utmc=80649031; __utmz=80649031.1556902975.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __atssc=google%3B2; __utma=80649031.910408159.1556902975.1557128827.1557138654.4; __utmt=1; __utmb=80649031.6.10.1557138654; __atuvc=44%7C18%2C8%7C19; __atuvs=5cd00cdf661a62bb004",
"Host": "www.ahpra.gov.au",
"Origin": "https://www.ahpra.gov.au",
"Referer": "https://www.ahpra.gov.au/Registration/Registers-of-Practitioners.aspx?m=Search&content_0$contentcolumnmain_0$txtFamilyName=aa",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
}
s.post(url, data=formdata,headers=headers)

我收到服务器错误并且没有结果,所以我想我错过了一些东西。当然有办法解决这个问题吗?

最佳答案

我认为问题与您发送formdata的方式有关。不要将其作为字符串发送,而是尝试将其作为 JSON 发送。

尝试替换这一行:

s.post(url, data=formdata,headers=headers)

这个:

s.post(url, json=formdata,headers=headers)

希望这有帮助

关于python - 如何使用 python 将表单发布到 aspx 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56004324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com