爬起点小说day03
# 把所有类别的前3页的小说爬取下来
import scrapy
from scrapy.http import Request
from time import sleep
from qidianNovel.spiders.connectionSQL import getredis,getMongodb
# 把起点首页的所有列表,起点是最后两页没有下一页(此处当做一页)
class spider_list_novel(scrapy.Spider):
name = "spider_list_novel" #要调用的名字
allowed_domains = ["qidian.com"] #分一个域
start_urls = []
dict = {}
red = getredis()
mongodb=getMongodb('novel','novels')
def __init__(self):
urls = self.red.lrange('bnovel_all_list', 0, -1)
for url in urls:
url = str(url, encoding="utf-8")
url = url.split(',')
spider_list_novel.start_urls.append(url[2])
spider_list_novel.dict[url[2]] ={'classId':url[0],'listId':url[1],'sum':0}
# break
#每爬完一个网页会回调parse方法
def parse(self, response):
print(response.url)
Pid = self.dict[response.url]
Pid['sum']+=1
print(Pid['sum'])
if Pid['sum']>3:
return
links = response.xpath('//div[@class="book-mid-info"]/h4/a')
for link in links:
novel_name = link.select("text()").extract()[0]
novel_id = self.mongodb.insert({'name': novel_name, 'total_list': Pid['classId'], 'list': Pid['listId']})
href = link.select("@href").extract()[0]
href = str(novel_id) + ',' + 'https:' + href
print(href)
self.red.lpush('all_novel_href',href)
sleep(0.3)
href=self.find_next(response)
if href==None:
f = open('file/%s.txt' % ("日志"), 'a', encoding='utf-8')
f.write(response.url)
f.write('++++++++++++++')
f.close()
else:
href="https:"+href
if href.find('javascript:;')<0:
self.dict[href] = Pid
request=Request(href,callback=self.parse)
yield request
def find_next(self,response):
try:
hrefs =response.xpath('//li[@class="lbf-pagination-item"]/a')
i=len(hrefs)
href=hrefs[i - 1].select("@href").extract()[0]
return href
except Exception as err:
f = open('file/%s.txt' % ("日志"), 'a', encoding='utf-8')
f.write(str(err)+':'+href)
f.close()
return None
爬起点小说day03的更多相关文章
- 爬起点小说 day01
先介绍下我自己爬起点小说的思路: 1.爬取所有的类型列表 a.链接存redis中 类型表:novel_list 具体每一种类型:bnovel_all_list(把novel_list和bnovel_l ...
- 爬起点小说 day02
总的来说起点小说还是挺好爬的,就是爬取小说的时候太慢了,4000多本小说就爬了2天一夜 首先爬取的是网页的所有类别,并把类别名存入到mongodb中,链接存到redis中: import scrapy ...
- Python实战项目网络爬虫 之 爬取小说吧小说正文
本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...
- python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)
转载出处:药少敏 ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...
- python之爬取小说
继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...
- 用Python爬取小说《一念永恒》
我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...
- Scrapy爬取小说简单逻辑
Scrapy爬取小说简单逻辑 一 准备工作 1)安装Python 2)安装PIP 3)安装scrapy 4)安装pywin32 5)安装VCForPython27.exe ........... 具体 ...
- 初次尝试python爬虫,爬取小说网站的小说。
本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...
- python3下BeautifulSoup练习一(爬取小说)
上次写博客还是两个月以前的事,今天闲来无事,决定把以前刚接触python爬虫时的一个想法付诸行动:就是从网站上爬取小说,这样可以省下好多流量(^_^). 因为只是闲暇之余写的,还望各位看官海涵:不足之 ...
随机推荐
- LINUX 笔记-grep命令
grep [-acinv] [--color=auto] '查找字符串' filename 它的常用参数如下: -a :将binary文件以text文件的方式查找数据 -c :计算找到'查找字符串'的 ...
- Mybatis基本用法--上
Mybatis基本用法--上 本文只是为自己查漏补缺.全面的请看官方文档,支持中英文 原理参考:http://blog.csdn.net/luanlouis/article/details/40422 ...
- 使用phpexcel类读写excel文件
使用原生php读写excel文件的博文地址: 基于使用原生php读写excel文件的不靠谱,本文将简单介绍如何使用第三方类库phpexcel来读写excel文件. 首先,需要到githut下载phpe ...
- Vue源码后记-更多options参数(1)
我是这样计划的,写完这个还写一篇数据变动时,VNode是如何更新的,顺便初探一下diff算法. 至于vue-router.vuex等插件源码,容我缓一波好吧,vue看的有点伤. 其实在之前讲其余内置指 ...
- SpringAop源码情操陶冶-AspectJAwareAdvisorAutoProxyCreator
本文将对SpringAop中如何为AspectJ切面类创建自动代理的过程作下简单的分析,阅读本文前需要对AOP的Spring相关解析有所了解,具体可见Spring源码情操陶冶-AOP之ConfigBe ...
- jQuery点击下拉菜单的展示与隐藏
首先点击显示某个div,然后要求再次点击时消失,或者点击document的其他地方会隐藏掉这个层,涉及到冒泡的问题,阻止document冒泡到dom上.代码如下: var $el = $(" ...
- Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合
前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...
- Android 开发笔记___SD卡基本操作
package com.example.alimjan.hello_world; /** * Created by alimjan on 7/5/2017. */ import android.ann ...
- C# into子句
可使用 into 上下文关键字创建临时标识符,将 group.join 或 select 子句的结果存储至新标识符. 此标识符本身可以是附加查询命令的生成器. 有时称在 group 或 select ...
- RobotFramework自动化测试框架-移动手机自动化测试Get Element Location关键字的使用
Get Element Location关键字用来获取一个Element的Location位置,该关键字接收一个参数[ locator ] 示例1:使用Get Element Location来获取一 ...