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

时间:2019-05-06 11:29:35

标签: python asp.net web-scraping beautifulsoup python-requests

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

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

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)

我收到服务器错误,但没有结果,所以我想我丢失了一些东西。当然可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为问题与您发送formdata的方式有关。与其以字符串形式发送,不如尝试以JSON形式发送。

尝试替换此行:

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

与此:

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

希望这会有所帮助