【问题标题】:Python urllib request always results in Error 400: Bad RequestPython urllib 请求总是导致错误 400:错误请求
【发布时间】:2021-12-23 09:22:39
【问题描述】:

感谢阅读。对于一个小型研究项目,我正在尝试从 KBB (www.kbb.com) 收集一些数据。但是,我总是收到“urllib.error.HTTPError:HTTP 错误 400:错误请求”错误。我想我可以用这段简单的代码访问不同的网站。我不确定这是代码问题还是特定网站本身的问题?

也许有人可以为我指明正确的方向。

from urllib import request as urlrequest
proxy_host = '23.107.176.36:32180'
url = "https://www.kbb.com/gmc/canyon-extended-cab/2018/"

req = urlrequest.Request(url)
req.set_proxy(proxy_host, 'https')

page = urlrequest.urlopen(req)
print(page)

【问题讨论】:

    标签: python web-scraping https urllib http-status-code-400


    【解决方案1】:

    我在下面找到了 2 个问题,但只有一个解决方案

    1. 是被拒绝的代理服务器。
    2. 您需要对服务器进行身份验证,以防它响应 403 禁止

    使用 urllib

    from urllib import request as urlrequest
    proxy_host = '23.107.176.36:32180'
    url = "https://www.kbb.com/gmc/canyon-extended-cab/2018/"
    
    req = urlrequest.Request(url)
    # req.set_proxy(proxy_host, 'https')
    
    page = urlrequest.urlopen(req)
    print(req)
    
    > urllib.error.HTTPError: HTTP Error 403: Forbidden
    

    使用请求

    import requests
    
    url = "https://www.kbb.com/gmc/canyon-extended-cab/2018/"
    
    res = requests.get(url)
    print(res)
    # >>> <Response [403]>
    

    使用 PostMan

    编辑解决方案

    设置超时垃圾更长的时间。但是 我不得不重试几次,因为代理有时只是不响应

    import urllib.request
    
    
    proxy_host = '23.107.176.36:32180'
    url = "https://www.kbb.com/gmc/canyon-extended-cab/2018/"
    
    proxy_support = urllib.request.ProxyHandler({'https' : proxy_host})
    opener = urllib.request.build_opener(proxy_support)
    urllib.request.install_opener(opener)
    
    res = urllib.request.urlopen(url, timeout=1000) # Set
    print(res.read())
    

    结果

    b'<!doctype html><html lang="en"><head><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=5,minimum-scale=1"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch preconnect" href="//securepubads.g.doubleclick.net" crossorigin><link rel="dns-prefetch preconnect" href="//c.amazon-adsystem.com" crossorigin><link .........
    

    使用请求

    import requests
    proxy_host = '23.107.176.36:32180'
    url = "https://www.kbb.com/gmc/canyon-extended-cab/2018/"
    
    # NOTE: we need a loger timeout for the proxy t response and set verify sale for an ssl error
    r = requests.get(url, proxies={"https": proxy_host}, timeout=90000,  verify=False) # Timeout are in milliseconds
    print(r.text)
    

    【讨论】:

    • 非常感谢。超时确实对我有用。
    • @dmort 你尝试了更多次吗?看起来每 5-8 次它会工作 1 次,另一次则失败。这意味着代理存在问题。即使它不应该与您提出的最初问题相关联。再试一次
    • 是的,我还必须尝试几次才能正常工作。我同意这是一个糟糕的代理服务器。它是“免费”代理,只是为了尝试这个。但是,在添加超时之前,它从来没有为我工作过一次,即使使用其他代理也是如此。所以这可能是问题的一部分:)
    【解决方案2】:

    如果没有 set_proxy 语句,您的代码似乎可以正常工作,我认为您的代理服务器很可能拒绝请求而不是 KBB。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2018-11-06
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多