【问题标题】:How to post a request to .aspx page with python如何使用 python 向 .aspx 页面发布请求
【发布时间】:2018-07-10 03:31:19
【问题描述】:

我正在尝试抓取以下网站:https://wwwapps.ncmedboard.org/Clients/NCBOM/Public/LicenseeInformationResults.aspx

为了让每个页面都被抓取,我需要先在这个 .aspx 页面上进行搜索,方法是输入名字和姓氏并启动搜索。

利用互联网上的资源,我整理了以下http POST请求:

url = 'https://wwwapps.ncmedboard.org/Clients/NCBOM/Public/LicenseeInformationResults.aspx'

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'en-GB,en;q=0.9,en-US;q=0.8,zh-TW;q=0.7,zh;q=0.6,zh-CN;q=0.5'
}

session = requests.session()
response = session.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'})
soup = BeautifulSoup(response.content, 'html.parser')
form_data = {
    '__VIEWSTATE': soup.find('input', {'name': '__VIEWSTATE'}).get('value'),
    '__VIEWSTATEGENERATOR': soup.find('input', {'name': '__VIEWSTATEGENERATOR'}).get('value'),
    'waLastName':'Smith',
    'waFirstName':'John',
    '__EVENTTARGET':'btnNext'
}

f = session.post(url, data=form_data, headers=headers)
soup = BeautifulSoup(f.content, 'html.parser')
for a in soup.find_all('a', href=True):
    print("Found the URL:" + a['href'])

post 似乎没有任何效果,因为当您在 post 请求后查看 html 时,它似乎没有显示结果页面。关于为什么会这样的任何指示?

谢谢!

【问题讨论】:

    标签: python asp.net beautifulsoup python-requests urllib


    【解决方案1】:

    您可能需要设置asp.net 会话cookie,它将为每个新会话生成新的。就您的网站而言 (https://wwwapps.ncmedboard.org) 是 ASP.NET_SessionId=(网站提供的 sessionid 的值)

    如果 CSRF 没有被正确验证,它可能会被绕过。

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 2011-09-10
      • 2013-05-12
      • 2013-01-15
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      相关资源
      最近更新 更多