【问题标题】:Incomplete response for XHR request in python with requests.get()使用 requests.get() 在 python 中对 XHR 请求的不完整响应
【发布时间】:2020-03-30 20:29:06
【问题描述】:

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

我想退回 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'

【问题讨论】:

    标签: python ajax python-3.x python-requests xmlhttprequest


    【解决方案1】:

    我能够使用 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])
    

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

    【讨论】:

      猜你喜欢
      • 2020-03-28
      • 2018-07-31
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2019-11-21
      • 1970-01-01
      • 2019-08-10
      相关资源
      最近更新 更多