【问题标题】:Scraping an ASPX session site with python requests, and a few redirects使用 python 请求和一些重定向抓取 ASPX 会话站点
【发布时间】:2020-06-01 21:19:15
【问题描述】:

我正在尝试为我拥有的设备抓取网站。该设备将其数据上传到该站点,该站点本身不提供 API 或任何其他编程方式供我访问此数据。

一个成功的看起来像这样(每个浏览器检查工具):

  1. 获取/站点/登录。
  2. 发布/站点/登录
  3. 302 到 /Site/Index
  4. 302 转 /Customer/Account
  5. 302 到 /Customer/Account/email%40domain.tld

这是我到目前为止的代码:

#!/usr/bin/python3

import requests
import json

payload = {
    "Email" : email,
    "Password" : password,
    "Remember" : 'false'
}
headers = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'Accept-Encoding': 'gzip, deflate, br',
  'Accept-Language' : 'en-US,en;q=0.5',
  'Content-Type': 'application/x-www-form-urlencoded',
  'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0',
}

s = requests.Session()

s.get('https://www.wifi.ecowater.com/Site/Login')

# Add the RequestVerificationToken to the payloay.
RVT = requests.utils.dict_from_cookiejar(s.cookies)
payload['__RequestVerificationToken'] = RVT['__RequestVerificationToken']

r = s.post('https://www.wifi.ecowater.com/Site/Login', params=payload, headers=headers)

print(r.text)

这段代码给我一个错误页面,几乎就像请求没有遵循所有重定向一样。

根据 Firefox 的浏览器检查工具,以下是上述 5 个步骤的 curl 命令。

编辑:尝试发布 5 个 curl 命令,但是当我这样做时,stackoverflow 将其标记为垃圾邮件,并且不允许我发布。

【问题讨论】:

    标签: python asp.net session curl cookies


    【解决方案1】:

    我在最初的问题中几乎有它,但在仔细查看网站后,我发现了一个隐藏的表单输入和第二个__RequestVerificationToken

    基本上,我根本不需要手动管理 __RequestVerificationToken cookie - requests.Session 会这样做 - 但我确实需要将隐藏的 __RequestVerificationToken 添加到表单数据负载中。

    我们最终得到两个不同的令牌,都名为__RequestVerificationToken。一个是服务器发送的,是cookie,另一个是表单数据,需要手动查找。

    这是我的工作代码:

    #!/usr/bin/python3
    
    import requests
    import re
    
    # Regex to match the hidden input on the initial log in page
    request_validation_re = re.compile(r'<input name="__RequestVerificationToken" type="hidden" value="(.*?)" />')
    
    # The serial number of your ecowater device
    dsn = { "dsn": "serial" }
    
    # The initial form data
    payload = {
        "Email" : "emailaddress",
        "Password" : "password",
        "Remember" : 'false'
    }
    
    # The headers needed for the JSON request
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language' : 'en-US,en;q=0.5',
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0'
    }
    
    with requests.Session() as s:
        # Initial GET request
        g = s.get('https://www.wifi.ecowater.com/Site/Login')
    
        # Grab the token from the hidden input
        tokens = request_validation_re.findall(g.text)
        # Add the token to the form data payload
        payload['__RequestVerificationToken'] = tokens[0]
    
        # Log in to the site
        login = s.post('https://www.wifi.ecowater.com/Site/Login', data=payload)
        # Add the correct Referer header
        headers['Referer'] = login.url + '/' + dsn['dsn']
    
        # Query the JSON endpoint for the data that we actually want
        data = s.post('https://www.wifi.ecowater.com/Dashboard/UpdateFrequentData', data=dsn, headers=headers)
        print(data.json())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2011-12-01
      • 2019-08-21
      相关资源
      最近更新 更多