python 爬蟲實戰(三)使用pyspider爬取虎嗅新聞
阿新 • • 發佈:2019-02-09
#!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2018-03-02 23:14:26 # Project: huxiu from pyspider.libs.base_handler import * class Handler(BaseHandler): crawl_config = { } @every(minutes=24 * 60) def on_start(self): self.crawl('https://www.huxiu.com/channel/105.html', callback=self.index_page,validate_cert=False) @config(age=10 * 24 * 60 * 60) def index_page(self, response): for each in response.doc('.mod-b.mod-art.clearfix h2 > a').items(): self.crawl(each.attr.href, callback=self.detail_page,validate_cert=False,fetch_type='js') @config(priority=2) def detail_page(self, response): return { "url": response.url, "title": response.doc('title').text()+' ', "detail":response.doc('.article-content-wrap > p').text() }