Scrapy爬取小说简单逻辑
Scrapy爬取小说简单逻辑
一 准备工作
1)安装Python
...........
具体安装步骤,可参考http://www.cnblogs.com/zyj-python/p/7392476.html
二 爬虫逻辑
cd Desktop(返回桌面目录) #选择文件保存位置,我放在了桌面
Scrapy startProject BooksSpider #BooksSpider为项目名称,自己起名
(ps:CMD终端显示"rou can start your first spider with:"表示项目创建成功)
拖动爬虫项目文件用Pycharm打开,点击左下角Terminal打开终端
scrapy genspider books(蜘蛛名,自定义,不能重复,可以修改但不建议修改) www.qisuu.com(网站域名, 这里以奇书网为例)
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import scrapy
import urlparse
from ..items import BooksItem
class BooksSpider(scrapy.Spider):
name = 'books'
allowed_domains = ['www.qisuu.com']
start_urls = ['http://www.qisuu.com/'] def parse(self, response):
pass
# -*- coding: utf- -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import scrapy
import urlparse
from ..items import BooksItem class BooksSpider(scrapy.Spider):
name = 'books'
allowed_domains = ['www.qisuu.com']
start_urls = ['http://www.qisuu.com/'] #获取首页导航条的url
def parse(self, response): a_list=response.xpath("//div[@class='nav']/a[@target='_blank']")
for a in a_list:
#分类名称
category_namme=a.xpath("text()").extract_first("")
#拼接完整的分类url
category_url=urlparse.urljoin(response.url,a.xpath("@href").extract_first("")) #yield将结果返回调度器
#将分类地址转发给downloader下载并将结果传给parse_books_list
#meta:专门用来传递参数,类型是字典
yield scrapy.Request(
url=category_url,
callback=self.parse_books_list,
meta={"category_namme":category_namme,}
)
#获取分类页面的所有url
def parse_books_list(self,response):
href_list=response.xpath("//div[@class='listBox']/ul/li/a/@href").extract()
for href in href_list:
list_href=urlparse.urljoin(response.url,href)
yield scrapy.Request(
url=list_href,
callback=self.parse_books_detail,
meta=response.meta,
# meta={"category_namme": response.meta['category_namme'],}
)
#获取所有页数,并循环获得每一页的url
all_pages=response.xpath("//select[@name='select']/option/@value").extract()
for page in all_pages:
detail_url=urlparse.urljoin(response.url,page)
yield scrapy.Request(
url=detail_url,
callback=self.parse_books_list,
meta=response.meta
)
#获取每个小说的详情
def parse_books_detail(self,response):
info_div=response.xpath("//div[@class='detail_right']")
title=info_div.xpath("h1/text()").extract_first("")
li_list=info_div.xpath("ul/li")
size=li_list[].xpath("text()").extract_first("")
size=size.replace(u"文件大小:","").strip()
date_time=li_list[].xpath("text()").extract_first("")
date_time=date_time.replace(u"发布日期:","").strip()
user=li_list[].xpath("a/text()").extract_first("")
download_times=li_list[].xpath("text()").extract_first("")
download_times = download_times.replace(u"下载次数:", "").strip()
book_degree=li_list[].xpath("em/@class").extract_first("")
book_degree = book_degree.replace("lstar","").strip()
download_url=response.xpath("//a[@class='downButton']/@href")[].extract()
img_url=response.xpath("//div[@class='detail_pic']/img/@src").extract_first("")
img_url=urlparse.urljoin(response.url,img_url)
category_namme=response.meta['category_namme']
print title,user,date_time,category_namme item=BooksItem()
item['title']=title
item['size']=size
item['date_time']=date_time
item['user']=user
item['download_times']=download_times
item['book_degree']=book_degree
item['download_url'] = [u"%s"%download_url] #当下在路径有乱码,加u 小说要以GBK格式存储,有中文时要进行编码
item['img_url']=[img_url]
item['category_namme']=category_namme
yield item
#yield 将结果返回给items.py文件
代码中的xpath与正则表达式是一样的,只是用法更加简单方便, 具体操作可百度, 此处不细说.
在items.py文件中
# -*- coding: utf-8 -*- # Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html import scrapy class BooksspiderItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# pass
class BooksItem(scrapy.Item):
title = scrapy.Field()
size = scrapy.Field()
date_time = scrapy.Field()
user = scrapy.Field()
download_times = scrapy.Field()
book_degree = scrapy.Field()
download_url = scrapy.Field()
img_url = scrapy.Field()
category_namme = scrapy.Field() #自定义一个类,用来接收获取到的数据
#Field()可以接受任何类型的参数
这时就可以开始爬虫了, 代码为:
scrapy crawl books -o book.json -s FEED_EXPORT_ENCODING = utf-8
其中 scrapy crawl books 是运行books爬虫程序, -o book.json 为以json格式保存, FEED_EXPORT_ENCODING = utf-8 为编码格式
友情提示: 不要轻易去爬虫,只有确定自己代码没有问题才可以,可以先使用终端测试(cmd),将代码一行一行依次粘贴运行
scrapy shell http://www.qisuu.com
如果想要下载至本地:
需要修改settings.py文件
ITEM_PIPELINES = {
# 'BooksSpider.pipelines.BooksspiderPipeline': 300,
"scrapy.pipelines.images.ImagesPipeline":1,
"scrapy.pipelines.files.FilesPipeline":2,
}
IMAGES_URLS_FIELD = "img_url"
IMAGES_STORE = "imgs"
FILES_URLS_FIELD = "download_url"
FILES_STORE = "files"
找到ITEM_PIPELINES,大约在67行,做出如上修改,IMAGES_URLS_FIELD获取下载图片的url, IMAGES_STORE新建一个文件夹,用来存放图片 FILES用法鱼IMAGES一致
可能存在的问题,原因及解决方案:
Scrapy爬取小说简单逻辑的更多相关文章
- scrapy爬取小说盗墓笔记
# -*- coding: utf-8 -*- import scrapy from daomu.items import DaomuItem class DaomuspiderSpider(scra ...
- scrapy 爬取小说
QiushuSpider # -*- coding: utf-8 -*- import scrapy import time from qiushu.items import QiushuItem c ...
- 小说免费看!python爬虫框架scrapy 爬取纵横网
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 风,又奈何 PS:如有需要Python学习资料的小伙伴可以加点击下方 ...
- Golang 简单爬虫实现,爬取小说
为什么要使用Go写爬虫呢? 对于我而言,这仅仅是练习Golang的一种方式. 所以,我没有使用爬虫框架,虽然其很高效. 为什么我要写这篇文章? 将我在写爬虫时找到资料做一个总结,希望对于想使用Gola ...
- Scrapy爬取美女图片第三集 代理ip(上) (原创)
首先说一声,让大家久等了.本来打算那天进行更新的,可是一细想,也只有我这样的单身狗还在做科研,大家可能没心思看更新的文章,所以就拖到了今天.不过忙了521,522这一天半,我把数据库也添加进来了,修复 ...
- 以豌豆荚为例,用 Scrapy 爬取分类多级页面
本文转载自以下网站:以豌豆荚为例,用 Scrapy 爬取分类多级页面 https://www.makcyun.top/web_scraping_withpython17.html 需要学习的地方: 1 ...
- scrapy 爬取纵横网实战
前言 闲来无事就要练练代码,不知道最近爬取什么网站好,就拿纵横网爬取我最喜欢的雪中悍刀行练手吧 准备 python3 scrapy 项目创建: cmd命令行切换到工作目录创建scrapy项目 两条命 ...
- scrapy爬取海量数据并保存在MongoDB和MySQL数据库中
前言 一般我们都会将数据爬取下来保存在临时文件或者控制台直接输出,但对于超大规模数据的快速读写,高并发场景的访问,用数据库管理无疑是不二之选.首先简单描述一下MySQL和MongoDB的区别:MySQ ...
- Scrapy爬取美女图片 (原创)
有半个月没有更新了,最近确实有点忙.先是华为的比赛,接着实验室又有项目,然后又学习了一些新的知识,所以没有更新文章.为了表达我的歉意,我给大家来一波福利... 今天咱们说的是爬虫框架.之前我使用pyt ...
随机推荐
- C#中的属性-Property
C#的属性一直都有用,但具体了解的不是很深,而且一些注意事项也没有太在意过,糊里糊涂的用着.这两天看了C#的书专门学习了一下属性,这才知道,原来属性也有这么多东西~ ~今天记录一下,算是对学习的一个检 ...
- itchat相关资料
https://itchat.readthedocs.io/zh/latest/ https://www.v2ex.com/t/306804 http://blog.csdn.net/th_num/a ...
- 论文阅读及复现 | Improved Semantic Representations From Tree-Structured Long Short-Term Memory Networks
两种形式的LSTM变体 Child-Sum Tree-LSTMs N-ary Tree-LSTMs https://paperswithcode.com/paper/improved-semantic ...
- 第六周课程总结&java实验报告四
第六周课程总结: 一.instanceof关键字 1.作用:在Java中可以使用instanceof关键字判断一个对象到底是哪个类的实例. 2.格式:对象 instanceof 类 -> 返回b ...
- flume部署
参考: 笔记 https://www.cnblogs.com/yinzhengjie/p/11183988.html 官网: http://flume.apache.org/documentation ...
- SQL SERVER DATEADD函数
定义: DATEADD() 函数在日期中加上指定的时间间隔. ※指定的时间间隔可以为负数 语法: DATEADD(datepart,number,date) 参数: ①datepart 参数可以是下列 ...
- Scala 内部类及外部类
转自:https://blog.csdn.net/yyywyr/article/details/50193767 Scala内部类是从属于外部类对象的. 1.代码如下 package com.yy.o ...
- SDL2 程序 编译 错误 及 解决方案
main函数应写为int main( int argc, char* args[] )而不是int main()形式 链接库时应注意顺序 mingw32;SDL2main;SDL2; ...
- redis 学习(19)-- RDB与AOF的抉择
RDB与AOF的抉择 1.RDB VS AOF RDB AOF 启动优先级 低 高 体积 小 大 恢复速度 快 慢 数据安全性 容易丢数据 根据策略决定 轻重 重 轻 2.RDB的最佳策略 关闭RDB ...
- 安装多个ORACLE导致多个Oracle HOME的情况!
Oracle由于版本的不同,在注册表中产生的注册表信息也有所不同,但主要的键值信息还是一样的,例如Oracle10g比oracle9i在注册表中表现的更为“简洁”,在未知的情况下,获取Oracle10 ...