python ร้องขอข้อผิดพลาด POST ปัญหาเซสชั่น?

Aug 25 2020

ฉันพยายามเลียนแบบการทำงานของเบราว์เซอร์ต่อไปนี้ผ่าน python's requests:

  1. ลงจอด https://www.bundesanzeiger.de/pub/en/to_nlp_start
  2. คลิก "ตัวเลือกการค้นหาเพิ่มเติม"
  3. คลิกที่ช่อง "นอกจากนี้ยังพบข้อมูล historicised" (สอดคล้องกับการโพสต์พระราม: 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

แต่เมื่อฉันตรวจสอบ url คำขอใน Chrome มันเป็นจริง

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

87นี่ดูเหมือนว่าจะมีการเปลี่ยนแปลงบอกมันเป็นบาง ID เซสชั่น แต่เมื่อฉันทำเช่นนี้ใช้requestsไม่ปรากฏในการแก้ปัญหาอย่างถูกต้อง

มีความคิดอะไรที่ฉันพลาดที่นี่?

คำตอบ

1 AndrejKesely Aug 25 2020 at 02:28

คุณสามารถลองใช้สคริปต์นี้เพื่อดาวน์โหลดไฟล์ 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.
idkhowtocode Aug 24 2020 at 23:56

หากคุณตรวจสอบ https://www.bundesanzeiger.de/robots.txtเว็บไซต์นี้ไม่ต้องการจัดทำดัชนี เว็บไซต์อาจปฏิเสธการเข้าถึงตัวแทนผู้ใช้เริ่มต้นที่บอทใช้ สิ่งนี้อาจช่วยได้: คำขอ Python กับ robots.txt