10-pyspider-frame-use

pyspider的基本使用

以爬取 去哪儿网 所有攻略的作者、标题、出发日期、人均费用、攻略正文、背景图片及呆了几天等信息。

1.cmd 运行 pyspider all

  1. 运行界面
  1. 调试页面
  1. 代码:
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2018-08-01 20:37:26
# Project: qunar

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):
#print(response)
for each in response.doc('li > .tit > a').items():
self.crawl(each.attr.href, callback=self.detail_page, fetch_type='js')
#print(each)
#print(each.attr.href)
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

}
分享到