python 请求 POST 错误,会话问题?

python requests POST error, session issue?

我试图通过 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 执行此操作时,它似乎无法正确解析。

知道我在这里遗漏了什么吗?

如果勾选https://www.bundesanzeiger.de/robots.txt, this website does not like to be indexed. The website could be denying access to the default user agent used by bots. This might help : Python requests vs. robots.txt

您可以试试这个脚本来下载 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.