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 ...
随机推荐
- Vue子组件调用父组件的方法
Vue子组件调用父组件的方法 Vue中子组件调用父组件的方法,这里有三种方法提供参考 第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法 父组件 <temp ...
- 基于w2v词向量聚类出现的问题(有待解决)
1.训练词向量代码如下:#训练词语为向量表示def w2v_train(self): ques = self.cu.execute('select question from activity')#将 ...
- Java 用HTTP的方式发送JSON报文请求
前言: 项目调用第三方接口时,通常是用socket或者http的通讯方式发送请求:http 为短连接,客户端发送请求都需要服务器端回送响应,请求结束后,主动释放链接.Socket为长连接:通常情况下S ...
- opencv手工编译
opencv手工编译方法1.下载cmake gui2.在where is the source code路径下配置opencv根目录,在where to build the binaries路径下配置 ...
- MySQL数据库改名的三种方法
前不久去面试,被问到Innodb引擎的表如何改数据库名,当时我也只回答了MyISAM改如何操作,被一些细节问题打败,真是操蛋. 如果表示MyISAM那么可以直接去到数据库目录mv就可以. Innodb ...
- tr 命令
[root@localhost .txt abcdefg [root@localhost .txt |tr [a-z] [A-Z] ABCDEFG // 把文件小写字母变成大写字母
- Python 全栈开发八 文件处理
一.基本流程 打开文件得到文件句柄 将文件句柄赋值给一个变量 通过文件句柄对文件进行操作 关闭文件 二.基本操作 1.文件句柄 f = open("a.txt",encoding= ...
- [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 661. Image Smoother_Easy
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- Linux 文件 目录结构探索
测试机用的是亚马逊云免费的那款,Amazon Linux AMI EC. 里面的Comment 在图上没有办法看,不过都是从网络上收录的,可以自行搜索. 目录 / 下面的目录 bin cgroup e ...