V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
baday
V2EX  ›  Python

关于 scrapy 用代理 IP 的问题

  •  
  •   baday · 2018-03-22 09:36:17 +08:00 · 3110 次点击
    这是一个创建于 2199 天前的主题,其中的信息可能已经有所发展或是发生改变。
    同样的代理 IP,我用 requests 时可以正常访问,用 scrapy 写中间件结果却是 10060 和 10061
    requests 代码
    def get_urls(url=START_URL):
    proxies = {"http": "http://{}".format("1.28.91.229:61202")}
    print(proxies)
    r = requests.get(url, proxies=proxies, headers=DEFAULT_REQUEST_HEADERS)
    etree = lxml.html.fromstring(r.text)
    # 获取界面关注人的路径
    urls = etree.xpath("//dl[@class='obu']/dd/a/@href")
    print(urls)

    中间件这样写的
    class ProxyMiddleware(object):
    '''设置代理 IP'''

    def process_request(self, request, spider):
    pro_adr = random.choice(PROXIES)
    print("USE PROXY -> " + pro_adr)
    request.meta["proxy"] = "http://" + pro_adr.strip()
    capljf
        1
    capljf  
       2018-03-22 10:12:41 +08:00
    代理失效了吧,换个代理试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4466 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 05:33 · PVG 13:33 · LAX 22:33 · JFK 01:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.