求助 关于 pyspider 使用多代理

2016-01-23 11:45:40 +08:00
 bytelee

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

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

谢谢!~~~

8281 次点击
所在节点    Python
8 条回复
ztrt
2016-01-23 13:19:48 +08:00
bytelee
2016-01-23 16:16:40 +08:00
多谢,居然忘了 @binux :)
bytelee
2016-01-23 16:28:43 +08:00
binux
2016-01-23 19:06:42 +08:00
设计就是用另一个代理管理代理池,比如用 squid 。一个软件做好自己的事就好了。
bytelee
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
2016-01-24 18:54:45 +08:00
@bytelee 没有 data:,_on_cronjob 吗?
bytelee
2016-01-24 23:32:18 +08:00
@binux 最新更新

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

所以可能是我在使用 config 的时候使用的有些问题。
qsnow6
2017-02-07 16:06:28 +08:00
非常感谢,这个也解决了我的配置问题,折腾了我两天了~~~~~`

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/252805

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX