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

2024-09-29 23:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在这个网站上用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)

我得到一个服务器错误,没有结果,所以我想我遗漏了一些东西。肯定有办法吗?你知道吗


Tags: ofhttpssearchapplicationwwwregistrationcontenten
1条回答
网友
1楼 · 发布于 2024-09-29 23:27:59

我认为这个问题与你如何发送formdata有关。不要将其作为字符串发送,而要尝试将其作为JSON发送。你知道吗

尝试替换此行:

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

用这个:

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

希望这有帮助

相关问题 更多 >

    热门问题