【问题标题】:python requests POST error, session issue?python请求POST错误,会话问题?
【发布时间】:2020-12-13 08:19:30
【问题描述】:

我正在尝试通过 python 的requests 模拟以下浏览器操作:

  1. 登陆https://www.bundesanzeiger.de/pub/en/to_nlp_start
  2. 点击“更多搜索选项”
  3. 点击复选框“同时查找历史数据”(对应于 POST 参数:isHistorical: true
  4. 点击“搜索净空头头寸”按钮
  5. 点击“Als CSV herunterladen”按钮下载csv文件

这是我必须模拟的代码:

import requests
import re

s = requests.Session()
r = s.get("https://www.bundesanzeiger.de/pub/en/to_nlp_start", verify=False, allow_redirects=True)

matches = re.search(
        r'form class="search-form" id=".*" method="post" action="\.(?P<appendtxt>.*)"',
        r.text
    )
request_url = f"https://www.bundesanzeiger.de/pub/en{matches.group('appendtxt')}"
sr = session.post(request_url, data={'isHistorical': 'true', 'nlp-search-button': 'Search net short positions'}, allow_redirects=True)

然而,即使sr 给了我一个status_code 200,当我检查sr.url 时它确实是一个错误,它显示https://www.bundesanzeiger.de/pub/en/error-404?9

再深入一点,我注意到上面的 request_url 解析为类似

https://www.bundesanzeiger.de/pub/en/nlp;wwwsid=EFEB15CD4ADC8932A91BA88B561A50E9.web07-pub?0-1.-nlp~filter~form~panel-form

但是当我在 Chrome 中检查请求 url 时,它实际上是

https://www.bundesanzeiger.de/pub/en/nlp?87-1.-nlp~filter~form~panel-form`

这里的87 似乎发生了变化,表明它是某个会话 ID,但是当我使用 requests 执行此操作时,它似乎无法正确解析。

知道我在这里缺少什么吗?

【问题讨论】:

    标签: python session web-scraping python-requests session-cookies


    【解决方案1】:

    如果你勾选https://www.bundesanzeiger.de/robots.txt,这个网站不喜欢被索引。该网站可能拒绝访问机器人使用的默认用户代理。这可能会有所帮助:Python requests vs. robots.txt

    【讨论】:

    • 我尝试添加:HEADERS = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)" " Chrome/50.0.2661.75 Safari/537.36", "X-Requested-With": "XMLHttpRequest", "Host": 'www.bundesanzeiger.de' } ,然后添加s.headers.update(HEADERS),但仍然遇到同样的问题。
    【解决方案2】:

    您可以尝试使用此脚本下载 CSV 文件:

    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://www.bundesanzeiger.de/pub/en/to_nlp_start'
    
    data = {
        'fulltext': '',
        'positionsinhaber': '',
        'ermittent': '',
        'isin': '',
        'positionVon': '',
        'positionBis': '',
        'datumVon': '',
        'datumBis': '',
        'isHistorical': 'true',
        'nlp-search-button': 'Search+net+short+positions'
    }
    
    headers = {
        'Referer': 'https://www.bundesanzeiger.de/'
    }
    
    with requests.session() as s:
        soup = BeautifulSoup(s.get(url).content, 'html.parser')
    
        action = soup.find('form', action=lambda t: 'nlp~filter~form~panel-for' in t)['action']
        u = 'https://www.bundesanzeiger.de/pub/en' + action.strip('.')    
    
        soup = BeautifulSoup( s.post(u, data=data, headers=headers).content, 'html.parser' )
    
        a = soup.select_one('a[title="Download as CSV"]')['href']
        a = 'https://www.bundesanzeiger.de/pub/en' + a.strip('.')    
    
        print( s.get(a, headers=headers).content.decode('utf-8-sig') ) 
    

    打印:

    "Positionsinhaber","Emittent","ISIN","Position","Datum"
    "Citadel Advisors LLC","LEONI AG","DE0005408884","0,62","2020-08-21"
    "AQR Capital Management, LLC","Evotec SE","DE0005664809","1,10","2020-08-21"
    "BlackRock Investment Management (UK) Limited","thyssenkrupp AG","DE0007500001","1,50","2020-08-21"
    "BlackRock Investment Management (UK) Limited","Deutsche Lufthansa Aktiengesellschaft","DE0008232125","0,75","2020-08-21"
    "Citadel Europe LLP","TAG Immobilien AG","DE0008303504","0,70","2020-08-21"
    "Davidson Kempner European Partners, LLP","TAG Immobilien AG","DE0008303504","0,36","2020-08-21"
    "Maplelane Capital, LLC","VARTA AKTIENGESELLSCHAFT","DE000A0TGJ55","1,15","2020-08-21"
    
    
    ...and so on.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      相关资源
      最近更新 更多