数据存放目录:
C:\Users\Administrator\data
升级版(可加载文章内所有多层嵌套的图片标签)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2019-04-08 14:24:34
# Project: qunaer from pyspider.libs.base_handler import * class Handler(BaseHandler):
crawl_config = {
} @every(minutes=24 * 60)
def on_start(self):
self.crawl('https://travel.qunar.com/travelbook/list.htm', callback=self.index_page,validate_cert=False) @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,validate_cert=False,fetch_type='js',js_viewport_height='')
next1=response.doc('.next').attr.href
self.crawl(next1,callback=self.index_page,validate_cert=False) @config(priority=2)
def detail_page(self, response):
imgs=response.doc('.js_memo_node').find('img')#获取id下的所有(包括多层嵌套的)img标签
img_list='' #必须事先声明,否则return,img_list时会报错:引用未事先声明的局部变量
for img in imgs.items():
img_list+=img.attr.src+',' #把所有图片用,组合在一起
#【复习】img_list=', '.join(['cats', 'rats', 'bats'])
return {
"url": response.url,
"title": response.doc('title').text(),
"date":response.doc('li.f_item.when > p > span.data').text(),
"day":response.doc('li.f_item.howlong > p > span.data').text(),
"text":response.doc('#b_panel_schedule').text(),
"img":img_list }
#ele-3076663-2 > div.bottom > div.e_img_schedule > div > dl:nth-child(2) > dt > img
例子A
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2019-04-08 14:24:34
# Project: qunaer from pyspider.libs.base_handler import * class Handler(BaseHandler):
crawl_config = {
} @every(minutes=24 * 60)
def on_start(self):
self.crawl('https://travel.qunar.com/travelbook/list.htm', callback=self.index_page,validate_cert=False) @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,validate_cert=False,fetch_type='js',js_viewport_height='')#用js加载,指定页面高度,防止懒加载图片只加载一半
next1=response.doc('.next').attr.href
self.crawl(next1,callback=self.index_page,validate_cert=False) @config(priority=2)
def detail_page(self, response):
#imgs=response.doc('#js_mainleft').find('img')
#for img in imgs.items():
# img_list=img_list+img+',' return {
"url": response.url,
"title": response.doc('title').text(),
"date":response.doc('li.f_item.when > p > span.data').text(),
"day":response.doc('li.f_item.howlong > p > span.data').text(),
"text":response.doc('#b_panel_schedule').text(),
"img":response.doc('#js_mainleft').find('img').attr.src }
#ele-3076663-2 > div.bottom > div.e_img_schedule > div > dl:nth-child(2) > dt > img