【问题标题】:How to fix "Max retries exceeded with url" error in requests lib?如何修复请求库中的“最大重试次数超出 url”错误?
【发布时间】:2022-06-12 22:48:08
【问题描述】:

我编写了一个在 AWS Lambdas 上运行的网络爬虫。它每 60 分钟运行一次。它工作了一年,但最近我开始遇到这种错误:

HTTPSConnectionPool(host='www.niederglatt-zh.ch', port=443): Max retries exceeded with url: /amtlichepublikationen (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f449f63a2d0>: Failed to establish a new connection: [Errno 111] Connection refused')) 

这是我要抓取的链接:

https://www.niederglatt-zh.ch/amtlichepublikationen

这是我的代码:

def sending_request(input_url):

    try:
        response = requests.get(input_url, allow_redirects=True, headers=get_random_header())
        print("Connection Response:", response, "Status Code:", response.status_code)
        if response.status_code != 200:
            time.sleep(random.randint(3, 7))
            response = requests.get(input_url, allow_redirects=True, headers=get_random_header(), verify = False, timeout=15)
            print("Connection Response - Second Try:", response)
    except:
        time.sleep(random.randint(4, 7))
        response = requests.get(input_url, allow_redirects=True, headers=get_random_header(), verify = False, timeout=15)
        print("Connection Response (verify == False):", response)    
    
    return response

我尝试过“玩”allow_redirects = True/Falsetimeoutverify==True/False 但我总是遇到同样的错误。

你可以忽略headers=get_random_header()

get_random_header() 是一个函数,它从用户代理列表中为我提供随机用户代理。

我还有抓取活动代理的脚本:

# LIST OF FREE PROXY APIs, THESE PROXIES ARE LAST TIME TESTED 60 MINUTES AGO, PROTOCOLS: HTTP, HTTPS, SOCKS4 AND SOCKS5
proxy_url = "https://proxylist.geonode.com/api/proxy-list?limit=200&page=1&sort_by=lastChecked&sort_type=desc&speed=fast"

# EXTRACTING JSON DATA FROM THIS LIST OF PROXIES

# Sending request to API
proxy_json = requests.get(proxy_url, headers=get_random_header()).text
proxy_json = json.loads(proxy_json)
full_proxy_list = proxy_json["data"]

# CREATING PROXY DICT
final_proxy_list = []
for proxy in full_proxy_list:

    #print(proxy) # JSON VALUE FOR ALL DATA THAT GOES INTO PROXY

    # Extracting protocol, ip address and port
    protocol = proxy['protocols'][0]
    if protocol != "https":
        ip_ = proxy['ip']
        port = proxy['port']

        # Creating PROXY dict
        proxy = {protocol : protocol + '://' + ip_ + ':' + port}

        final_proxy_list.append(proxy)

我尝试将随机代理传递给这样的代码:

response = requests.get(input_url, allow_redirects=True, headers=get_random_header(), proxies = random.choice(final_proxy_list))

但我仍然遇到同样的错误

有没有办法修复这个错误?

谢谢:)

【问题讨论】:

  • “连接被拒绝”错误意味着远程主机简单地拒绝了连接尝试;它与您请求的内容无关,因为从未发送过任何内容。也许远程端有某种速率限制,在请求过多后将您的 ip 添加到阻止列表?
  • 我正在使用 AWS Lambda 服务进行爬取。我将 cron 作业设置为每 60 分钟运行一次。据我所知(如果我错了,请纠正我),每个 lambda 都有不同的 IP 地址。该网站是否有可能阻止所有 AWS IP 地址?有没有办法更改 AWS lambda 函数的 IP 地址?

标签: python amazon-web-services lambda request web-crawler


猜你喜欢
  • 2017-06-22
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
相关资源
最近更新 更多