pyspider 示例二 升级完整版绕过懒加载,直接读取图片
pyspider 示例二 升级完整版绕过懒加载,直接读取图片,见【升级写法处】
#!/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='100') 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('data-original')+',' #【升级写法】,绕过懒加载直接读取图片真正地址 #htm源码<img data-original="https://tr-osdcp.qunarzz.com/tr-osd-tr-space/img/ee.jpg" src="layz-load"> 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