pyspider 示例
数据存放目录:
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
pyspider 示例的更多相关文章
- pyspider示例代码:解析JSON数据
pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下比较经典的示例进行简单讲解,希望对新手有一些帮助. 示例说明: py ...
- pyspider 示例二 升级完整版绕过懒加载,直接读取图片
pyspider 示例二 升级完整版绕过懒加载,直接读取图片,见[升级写法处] #!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on ...
- pyspider示例代码三:用PyQuery解析页面数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一些 ...
- pyspider示例代码二:解析JSON数据
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...
- pyspider示例代码一:利用phantomjs解决js问题
本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...
- pyspider示例代码六:传递参数
传递参数 示例一 #!/usr/bin/env python # -*- encoding: utf- -*- # vim: ts= sts= ff=unix fenc=utf8: # Created ...
- pyspider示例代码五:实现自动翻页功能
实现自动翻页功能 示例代码一 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: v2ex fro ...
- pyspider示例代码七:自动登陆并获得PDF文件下载地址
自动登陆并获得PDF文件下载地址 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: pdf_sp ...
- pyspider示例代码四:搜索引擎爬取
搜索引擎爬取 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: __git_lab_fix fr ...
随机推荐
- mysql拼接sql的语法concat()用法
之前写了oracle拼接sql是用“||”,那么mysql也有自己的拼接的语法concat() concat()的基本语法是如下: 括号内的拼接内容语法和python中拼接显示字符串和变量.常量时一样 ...
- Ubuntu14.04 LTS 安装Chrome浏览器(转)
add zhj: 亲测过,可以,原来不用FQ就可以下载,有点意外 原文:http://www.jianshu.com/p/8220578d0b15 1.打开终端(ctrl + alt + t),下载6 ...
- oracle查看哪些表被锁
select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where a ...
- try catch和spring事务
- IGMP协议
IGMP报文格式: 4bit的IGMP版本(1)+4bit的IGMP类型(1-2)+8bit未用+16bit检验和(同ICMP)+32bit组地址(D类IP地址) 类型为1说明是由多播路由器发出的查询 ...
- OC常用控件封装
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface CreateUI : NSObject ...
- if __name__==__main__:的应用
1. if __name__==__main__:代码的作用 python的文件有两种使用的方法:(1)直接作为脚本执行:(2)import到其他的python脚本中被调用(模块重用)执行: 因此if ...
- Mac上安装PHP、Apache、MySQL
Mac自带php5.6版本,要升级到php7.3 步骤如下 1,brew 安装php ,如果没有安装,访问https://brew.sh/index_zh-cn安装在终端输入以下内容,不用指定安装ph ...
- SecureCRT乱码问题的解决
== 安装后默认的设置是utf8,不是汉字乱码,是一块一块的看不清,OMG... 设置这两个页面就好了
- [LeetCode] 590. N-ary Tree Postorder Traversal_Easy
Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...