pyspider的基本使用
以爬取 去哪儿网 所有攻略的作者、标题、出发日期、人均费用、攻略正文、背景图片及呆了几天等信息。
1.cmd 运行 pyspider all
- 运行界面
- 调试页面
- 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
from pyspider.libs.base_handler import *
class Handler(BaseHandler): crawl_config = { }
@every(minutes=24 * 60) def on_start(self): self.crawl('http://travel.qunar.com/travelbook/list.htm', callback=self.index_page)
@config(age=10 * 24 * 60 * 60) def index_page(self, response): for each in response.doc('li > .tit > a').items(): self.crawl(each.attr.href, callback=self.detail_page, fetch_type='js') next = response.doc('.next').attr.href self.crawl(next, callback=self.index_page)
@config(priority=2) def detail_page(self, response): return { "url": response.url, "title": response.doc('#booktitle').text(), "author": response.doc('.who .data').text(), "date": response.doc('.when .data').text(), "days": response.doc('.howlong .data').text(), "money": response.doc('.howmuch .data').text(), "text": response.doc('#b_panel_schedule').text(), "img": response.doc('.cover_img').attr.src }
|