【问题标题】:timeout not respected in request.get()request.get() 中不遵守超时
【发布时间】:2020-04-04 03:00:59
【问题描述】:

我不明白为什么使用此代码requests.get() 会立即返回错误并且不遵守 10 秒超时。 如果我删除了标头参数,所以只有requests.get(url, proxies={"http": proxy, "https": proxy}, verify=False,timeout=10),超时会受到尊重,如果发生超时,则会成功返回请求或失败。 我还需要插入标头和超时来测试代理,怎么做?

from fake_useragent import  UserAgent
import requests
ua = UserAgent()

#get a list of proxy 

for i in range(1, len(proxies)):
    # Get a proxy from the pool
    proxy = next(proxy_pool)
    print("Request #%d" % i)
    try:
        response = requests.get(url, headers=ua.random(), proxies={"http": proxy, "https": proxy}, verify=False,timeout=10)
        break
    except:
        print("Skipping. Connnection error")

【问题讨论】:

  • 如果你用 headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/ 537.17" ???
  • 是的,它有效,所以现在我认为 ua.random() 会生成错误的用户代理。你知道更好的包吗?或者可靠的用户代理列表? @GiovaniSalazar
  • 我在下面回复了

标签: python python-requests timeout


【解决方案1】:

试试这个

ua = UserAgent()
header = {'User-Agent':str(ua.random)}
response = requests.get(url, headers=header, verify=False,timeout=10)

【讨论】:

    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2020-05-02
    • 2020-07-18
    • 2010-09-06
    • 2018-03-30
    • 1970-01-01
    相关资源
    最近更新 更多