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
bytelee
V2EX  ›  Python

求助 关于 pyspider 使用多代理

  •  
  •   bytelee · 2016-01-23 11:45:40 +08:00 · 8246 次点击
    这是一个创建于 2987 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近玩 pyspider 练习,打算试试他的代理。但是文档里边只是看到了单个代理的设置。也就是在 config 或者 crawl_config 。

    但是我想试试他在多代理是否表现的不错,现在想的方案就是用 squid 来过一下。但是没有找到这方面的资料。请问大家这个该怎么做?直接启动 squid 然后把 pysider 的 fetcher 加上 squid 就行了么?

    谢谢!~~~

    8 条回复    2017-02-07 16:06:28 +08:00
    ztrt
        1
    ztrt  
       2016-01-23 13:19:48 +08:00 via Android
    bytelee
        2
    bytelee  
    OP
       2016-01-23 16:16:40 +08:00
    多谢,居然忘了 @binux :)
    bytelee
        3
    bytelee  
    OP
       2016-01-23 16:28:43 +08:00   ❤️ 1
    binux
        4
    binux  
       2016-01-23 19:06:42 +08:00
    设计就是用另一个代理管理代理池,比如用 squid 。一个软件做好自己的事就好了。
    bytelee
        5
    bytelee  
    OP
       2016-01-24 11:42:37 +08:00
    @binux 是的 多谢提醒 昨天测试没问题 很赞的工具。不过有一个疑问,关于 every 和 age 的使用。我看文档不是很多。现在我用 itag 每次修改完后强制重新 load 一次。但是有个问题是这之后就一直不自动运行了,经常输出:

    pyspider task done :on_start data:,on_start

    是不是我的配置有问题?

    from pyspider.libs.base_handler import *
    import re
    import datetime

    class Handler(BaseHandler):
    crawl_config = {
    "itag":"v0.0.6"
    }

    @every(minutes=3)
    @config(age=3*60)
    def on_start(self):
    self.crawl('http://www..com.cn/', callback=self.index_page)

    @every(minutes=3)
    @config(age=3*60)
    def index_page(self, response):
    for each in response.doc('a[href^="http"]').items():
    href = each.attr.href
    if re.match("http://www.xxxx.com.cn/gonglu/[^_]*/", href, re.U):
    self.crawl(each.attr.href, callback=self.detail_page,save={'main_road_name':each.text()}, retries=10, auto_recrawl =True)

    def detail_page(self, response):
    road_name = response.save.get('main_road_name', '')
    road_img_url = ""

    for img_item in response.doc('td.hcenter > img').items():
    road_img_url = img_item.attr.src

    for each in response.doc('.roadlineB > li > a').items():
    href = each.attr.href
    if re.match("http://www.xxx.com.cn/gonglu/gaosu_[^cs]\w*/", href, re.U):
    city_road_name = each.text()
    self.crawl(each.attr.href, callback=self.detail_page,save={'main_road_name':road_name, 'city_road_name':city_road_name}, retries =10, auto_recrawl=True)

    messages = []
    for each in response.doc(".LKlistleftE > p").items():
    message = each.text()
    info = self.process_message(message)
    if info:
    messages.append(info)
    city_road_name = response.save.get('city_road_name', '')
    return dict(main_road_name=road_name, city_road_name=city_road_name, messages=messages, road_img_url=road_img_url)

    def process_message(self, message):
    return dict(content=message)

    这是我的代码,不知道是不是有问题。

    谢谢
    binux
        6
    binux  
       2016-01-24 18:54:45 +08:00
    @bytelee 没有 data:,_on_cronjob 吗?
    bytelee
        7
    bytelee  
    OP
       2016-01-24 23:32:18 +08:00
    @binux 最新更新

    我尝试去掉了 @config 这些,直接在 self.craw 里边 加了 age 和 auto recaw,现在基本没问题了。

    所以可能是我在使用 config 的时候使用的有些问题。
    qsnow6
        8
    qsnow6  
       2017-02-07 16:06:28 +08:00
    非常感谢,这个也解决了我的配置问题,折腾了我两天了~~~~~`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1041 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:32 · PVG 06:32 · LAX 15:32 · JFK 18:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.