在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ scrapy使用代理ip抓取數(shù)據(jù)時關(guān)于超時與連接的若干報錯

scrapy使用代理ip抓取數(shù)據(jù)時關(guān)于超時與連接的若干報錯

在使用scrapy添加代理ip時去抓取數(shù)據(jù),查看日志中的報錯不甚其解,希望能有大神能為我指出更詳細的原因

報錯信息:
1、twisted.internet.error.TimeoutError: User timeout caused connection failure: Getting http://open.douyucdn.cn/api/RoomApi/room/1355623 took longer than 180.0 seconds.

2、twisted.internet.error.ConnectionRefusedError: Connection was refused by other side: 111: Connection refused

3、twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]

4、twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure twisted.internet.error.ConnectionLost: Connection to the other side was lost in a non-clean fashion.>]

附上我寫的代理中間件
middlewares.py

class ZhimaProxyMiddleware(object):
    '''芝麻代理中間件'''
    def __init__(self):
        with open("ip_pool.txt", "r") as f:
            self.proxy_dict = json.loads(f.read())
            self.proxy_list = self.proxy_dict["proxy"]

    def process_request(self, request, spider):
        try:
            if request.url.find("douyu") > 0:
                request.meta["proxy"] = random.choice(self.proxy_list)["http"]
        except ValueError as error:
            logging.error("芝麻代理賬戶余額不足,{}".format(error))
        finally:
            return None

代理的提取是經(jīng)過檢測的
附上檢測的代碼

import requests
import json

# 芝麻代理接口
zhima_url = ""
# 測試代理有效性url
test_url = "http://www.qq.com/"
# 定義空列表
zhima = {"proxy": []}

def zhima_proxy():
    '''獲取芝麻代理'''
    response = requests.get(zhima_url)
    proxy_dict = json.loads(response.content.decode())

    for data in proxy_dict["data"]:
        proxy_ip = data["ip"]
        proxy_port = data["port"]
        proxy = {
            "http": "http://{}:{}".format(proxy_ip, proxy_port),
            "https": "https://{}:{}".format(proxy_ip, proxy_port)
        }
        try:
            res = requests.get(test_url, proxies=proxy, verify=False)
        except Exception as error:
            print("代理無法連接".format(error))
            continue
        else:
            if res.status_code == 200:
                zhima["proxy"].append(proxy)
            else:
                continue
    with open("ip_pool.txt", "w") as f:
        f.write(json.dumps(zhima))

用線上代理檢測工具也檢測出代理都是有效的
可是還是不知道為什么會出現(xiàn)這些問題

確定中間件已開啟
感謝

回答
編輯回答
避風港

代理可能過時了

2018年3月23日 15:25
編輯回答
別瞎鬧

在使用的時候再檢測一遍代理能用不

2018年7月4日 17:14
編輯回答
心沉

我和你一樣,但是我用的是蘑菇代理,你怎么解決的?

2017年8月14日 07:09