繁体   English   中英

使用 requests.get() 在 python 中对 XHR 请求的响应不完整

[英]Incomplete response for XHR request in python with requests.get()

我正在尝试使用服务器上的 Python 请求为给定城市中的给定街道抓取德国邮政编码 (PLZ)。 我正在尝试应用我在这里学到的知识。

我想归还Schanzäckerstr.的 PLZ Schanzäckerstr. Nürnberg

import requests

url = 'https://www.11880.com/ajax/getsuggestedcities/schanz%C3%A4ckerstra%C3%9Fe%20n%C3%BCrnberg?searchString=schanz%25C3%25A4ckerstra%25C3%259Fe%2520n%25C3%25BCrnberg'

data = 'searchString=schanz%25C3%25A4ckerstra%25C3%259Fe%2520n%25C3%25BCrnberg'


headers = {"Authority": "wwww.11880.com",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0",
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "Accept-Language": "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7",
                "Accept-Encoding": "gzip, deflate, br",
                "X-Requested-With": "XMLHttpRequest",
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "Content-Length": "400",
                "Origin": "https://www.postleitzahlen.de",
                "Sec-Fetch-Site": "cross-site",
                "Fetch-Mode": "cors",
                "DNT": "1",
                "Connection": "keep-alive",
                "Referer": "https://www.postleitzahlen.de",
               }
multipart_data = {(None, data,)}

session = requests.Session()
response = session.get(url, files=multipart_data, headers=headers)

print(response.text)

上面的代码产生了一个 200 类型的空响应。我想返回:

'90443'

我能够使用 nominatim openstreetmap API 解决这个问题。 还可以添加街道号码

import requests
city = 'Nürnberg'
street = 'Schanzäckerstr. 2'

response = requests.get( 'https://nominatim.openstreetmap.org/search', headers={'User-Agent': 'PLZ_scrape'}, params={'city': city, 'street': street[1], 'format': 'json', 'addressdetails': '1'}, )
print(street, ',', [i.get('address').get('postcode') for i in response.json()][0])

确保每秒只发送一个请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM