scrapy之盗墓笔记三级页面爬取
#今日目标 **scrapy之盗墓笔记三级页面爬取** 今天要爬取的是盗墓笔记小说,由分析该小说的主要内容在三级页面里,故需要我们
一一解析 *代码实现* daomu.py ```
import scrapy
from ..items import DaomuItem class DaomuSpider(scrapy.Spider):
name = 'daomu'
allowed_domains = ['daomubiji.com']
start_urls = ['http://www.daomubiji.com/'] # 解析一级页面的parse函数
def parse(self, response):
# link_list: ['http://xxx/dao-mu-bi-ji-1','','','']
link_list = response.xpath('//ul[@class="sub-menu"]/li/a/@href').extract()
for link in link_list:
# 交给调度器
yield scrapy.Request(
url = link,
callback = self.parse_two_html
) # 解析二级页面函数(圈名 章节数 章节名 链接)
def parse_two_html(self,response):
# 基准xpath
article_list = response.xpath('//article')
for article in article_list:
# 创建item对象
item = DaomuItem()
# info_list: ['七星鲁王','第一章','血尸']
info_list = article.xpath('./a/text()').get().split()
if len(info_list) == 3:
item['volume_name'] = info_list[0]
item['zh_num'] = info_list[1]
item['zh_name'] = info_list[2]
else:
item['volume_name'] = info_list[0]
item['zh_name'] = info_list[1]
item['zh_num'] = '' # 提取链接并发给调度器入队列
item['zh_link'] = article.xpath('./a/@href').get()
yield scrapy.Request(
url = item['zh_link'],
# meta参数: 传递item对象到下一个解析函数
meta = {'item':item},
callback = self.parse_three_html
) # 解析三级页面(小说内容)函数
def parse_three_html(self,response):
# 获取上一个函数传递过来的item对象
item = response.meta['item']
# content_list: ['段落1','段落2','','']
content_list = response.xpath(
'//article[@class="article-content"]//p/text()'
).extract() item['zh_content'] = '\n'.join(content_list) yield item ``` items.py ``` import scrapy class DaomuItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# 卷名
volume_name = scrapy.Field()
# 章节数
zh_num = scrapy.Field()
# 章节名称
zh_name = scrapy.Field()
# 章节链接
zh_link = scrapy.Field()
# 小说内容
zh_content = scrapy.Field() ``` pipelines.py ```
class DaomuPipeline(object):
def process_item(self, item, spider): filename = '/home/tarena/daomu/{}_{}_{}'.format(
item['volume_name'],
item['zh_num'],
item['zh_name']
) with open(filename,'w') as f:
f.write(item['zh_content']) return item ```
scrapy之盗墓笔记三级页面爬取的更多相关文章
- 【图文详解】scrapy爬虫与动态页面——爬取拉勾网职位信息(2)
上次挖了一个坑,今天终于填上了,还记得之前我们做的拉勾爬虫吗?那时我们实现了一页的爬取,今天让我们再接再厉,实现多页爬取,顺便实现职位和公司的关键词搜索功能. 之前的内容就不再介绍了,不熟悉的请一定要 ...
- 爬虫系列5:scrapy动态页面爬取的另一种思路
前面有篇文章给出了爬取动态页面的一种思路,即应用Selenium+Firefox(参考<scrapy动态页面爬取>).但是selenium需要运行本地浏览器,比较耗时,不太适合大规模网页抓 ...
- 爬虫系列4:scrapy技术进阶之多页面爬取
多页面爬取有两种形式. 1)从某一个或者多个主页中获取多个子页面的url列表,parse()函数依次爬取列表中的各个子页面. 2)从递归爬取,这个相对简单.在scrapy中只要定义好初始页面以及爬虫规 ...
- scrapy中使用selenium来爬取页面
scrapy中使用selenium来爬取页面 from selenium import webdriver from scrapy.http.response.html import HtmlResp ...
- 使用requests简单的页面爬取
首先安装requests库和准备User Agent 安装requests直接使用pip安装即可 pip install requests 准备User Agent,直接在百度搜索"UA查询 ...
- python爬爬爬之单网页html页面爬取
python爬爬爬之单网页html页面爬取 作者:vpoet mail:vpoet_sir@163.com 注:随意copy 不用告诉我 #coding:utf-8 import urllib2 Re ...
- Scrapy 通过登录的方式爬取豆瓣影评数据
Scrapy 通过登录的方式爬取豆瓣影评数据 爬虫 Scrapy 豆瓣 Fly 由于需要爬取影评数据在来做分析,就选择了豆瓣影评来抓取数据,工具使用的是Scrapy工具来实现.scrapy工具使用起来 ...
- python3编写网络爬虫14-动态渲染页面爬取
一.动态渲染页面爬取 上节课我们了解了Ajax分析和抓取方式,这其实也是JavaScript动态渲染页面的一种情形,通过直接分析Ajax,借助requests和urllib实现数据爬取 但是javaS ...
- Python Requests库入门——应用实例-京东商品页面爬取+模拟浏览器爬取信息
京东商品页面爬取 选择了一款荣耀手机的页面(给华为打广告了,荣耀play真心不错) import requests url = "https://item.jd.com/7479912.ht ...
随机推荐
- hdu_1712(dp,背包)
hdu_1712 \[dp[i][j]\] 表示前i个物品用了j天得到的最大收益 \[ dp[i][j] = max(dp[i-1][j],dp[i][j-k]+ k*v[i][k]) \qquad ...
- python学习之路(19)
匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x) ...
- 一致性Hash 分析和实现
一致性Hash 分析和实现 ---title: 1.一致性Hashdate: 2018-02-05 12:03:22categories:- 一致性Hash--- 一下分析来源于网络总结:算法参照自己 ...
- spark streaming 3: Receiver 到 submitJobSet
对于spark streaming来说,receiver是数据的源头.spark streaming的框架上,将receiver替换spark-core的以磁盘为数据源的做法,但是数据源(如监听某个 ...
- DB2基础维护手册
诊断DB2系统性能:db2top -d DEMODB db2top详解:http://blog.sina.com.cn/s/blog_636d62310102v7lm.html
- KNN距离函数的简单拓展
KNN--k-NearestNeighbor可以是是分类法中最简单的算法了. 大致的idea为:找出k各跟新数据点最像的点,看这些点主要属于哪类,那么新数据点也就属于哪类的了. 其伪代码如下: 1. ...
- OpenStack 2018 年终盘点
目录 文章目录 目录 前言 OpenStack 一年来的成长 Nova Cinder Neutron Ironic Cyborg Octavia Kolla Magnum Zun Kuryr 从 Op ...
- 清除陷入CLOSE_WAIT的进程
netstat -nap |grep :8009|grep CLOSE_WAIT | awk '{print $7}'|awk -F"\/" '{print $1}' |awk ' ...
- Haproxy 代理
一:安装haproxy 1:解压 编译 安装 tar zxf haproxy-1.7.9.tar.gz cd haproxy-1.7.9 uname -e make TARGET=linux ...
- 如何实现在Eclipse导入Apache Commons
Apache Commons https://en.wikipedia.org/wiki/Apache_Commons 右键项目->Properties->Java Build Path- ...