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 ...
随机推荐
- rar压缩类
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...
- django基础-02:虚拟环境
4. 虚拟环境 4.1 概念 之前安装python包的命令: sudo pip3 install 包名 包的安装路径:/usr/local/lib/python3.5/dist-packages 在同 ...
- 报错解决——make: *** No targets specified and no makefile found. Stop
wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz tar zxvf ncurses-5.6.tar.gz #切换目录到ncurses ...
- 清空select标签中option选项的3种不同方式
方法一 代码如下:document.getElementById("selectid").options.length = 0; 方法二 代码如下:document.formNam ...
- 小程序 login
app.json : 配置文件 => 文件路径 pages .配置窗口 window.底部导航 tabBar .请求超时时间 networkTimeout app.js : 请求路口文件 wx. ...
- IconMoon图标字体制作
官网:https://icomoon.io/ 点击右上角“IconMoon APP” 点击左上角“import Icons”按钮 选中小图标 - 选择右下角“Generate Font”生成图标字体 ...
- Tx.Origin 用作身份验证
Solidity 中有一个全局变量,tx.origin,它遍历整个调用栈并返回最初发送调用(或交易)的帐户的地址.在智能合约中使用此变量进行身份验证会使合约容易受到类似网络钓鱼的攻击. 有关进一步阅读 ...
- 对接口(interface)的思考
接口,java中用关键字interface定义.今天学习java基础API时,发现一个特点:每个包中都定义了一堆的接口,感觉有马克思主义中提到的“上层建筑”的味道.接口就是为需要实现的功能定一个基调, ...
- python string method
嗯,学习其它语言没这样全练过,嘻嘻 //test.py 1 # -*- coding: UTF-8 -*- 2 3 str = "i am worker" 4 print str. ...
- iOS UI基础-9.2 UITableView 简单微博列表
概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...