【问题标题】:How to post a form to aspx site using python如何使用 python 将表单发布到 aspx 站点
【发布时间】:2019-05-06 11:29:34
【问题描述】:

我正在尝试在此站点上使用 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)

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

【问题讨论】:

  • 您的表单数据中仍有一些字段丢失,如__VIEWSTATE1,__VIEWSTATEGENERATOR,__EVENTVALIDATION 等。你试过包括他们吗?
  • @SIM 感谢我忘记包含这些,现在一切都按预期工作。您可以将此作为答案,我会将其标记为已解决。干杯

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


【解决方案1】:

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

尝试替换这一行:

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

用这个:

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

希望对你有帮助

【讨论】:

  • 谢谢,在关注@SIM 上面的评论后,我现在可以正常工作了。 json=formdata 再次返回搜索表单但没有结果,所以我改用 data=formdata
猜你喜欢
  • 2010-09-17
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
相关资源
最近更新 更多