2

现在我想在这个网页上刮掉所有物品(iphone)的图像。首先我把图片的所有链接都解压出来,然后一个一个的发送请求到src,下载到文件夹'/phone/'。这是我的代码:

from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
    }

    @every(minutes=24 * 60)
    def on_start(self):
        print 'hi'
        self.crawl('https://s.taobao.com/search?q=iphone&imgfile=&ie=utf8', callback=self.index_page, fetch_type='js')

    #@config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        items = response.doc('.item').items()
        for item in items:
            imgurl = item('.J_ItemPic img').attr('.src')
            if imgurl:
                filename = item('.J_ItemPic.img').attr('.id')
                self.crawl(imgurl, callback=self.scrape_photo, save={'filename': filename})

    def save_photo(self, content, filename):
        with open('phone/'+filename, 'wb') as f:
            f.write(content)

    def scrape_photo(self, response):
        content = response.content
        filename = response.save['filename']+'.jpg'
        self.save_photo(content, filename)

它非常直观和简单。但是当我运行代码时,什么都没有发生,我只是在终端中收到了以下日志消息:

[I 160602 18:57:42 scheduler:664] restart task sk:on_start data:,on_start
[I 160602 18:57:42 scheduler:771] select sk:on_start data:,on_start
[I 160602 18:57:42 tornado_fetcher:178] [200] sk:on_start data:,on_start 0s
[I 160602 18:57:42 processor:199] process sk:on_start data:,on_start -> [200] len:8 -> result:None fol:1 msg:0 err:None
[I 160602 18:57:42 scheduler:712] task done sk:on_start data:,on_start

我对这个问题几乎疯了。你能告诉我是什么问题,我该如何解决?提前致谢!

4

1 回答 1

1

你之前爬过链接“ https://s.taobao.com/search?q=iphone&imgfile=&ie=utf8 ”吗?

默认情况下,pyspider 将丢弃已抓取的链接(您的评论@config(age=10 * 24 * 60 * 60)意味着永远不会重新抓取)

如果您想重新启动 hold 项目http://docs.pyspider.org/en/latest/apis/self.crawl/#itag会有所帮助。

于 2016-06-02T18:16:28.883 回答