數據存放目錄:node
C:\Users\Administrator\datapython
升級版(可加載文章內全部多層嵌套的圖片標籤)ide
#!/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='100000') 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
例子Afetch
#!/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='100000')#用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