# 把所有类别的前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的更多相关文章

  1. 爬起点小说 day01

    先介绍下我自己爬起点小说的思路: 1.爬取所有的类型列表 a.链接存redis中 类型表:novel_list 具体每一种类型:bnovel_all_list(把novel_list和bnovel_l ...

  2. 爬起点小说 day02

    总的来说起点小说还是挺好爬的,就是爬取小说的时候太慢了,4000多本小说就爬了2天一夜 首先爬取的是网页的所有类别,并把类别名存入到mongodb中,链接存到redis中: import scrapy ...

  3. Python实战项目网络爬虫 之 爬取小说吧小说正文

    本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...

  4. python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)

    转载出处:药少敏   ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...

  5. python之爬取小说

    继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...

  6. 用Python爬取小说《一念永恒》

    我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...

  7. Scrapy爬取小说简单逻辑

    Scrapy爬取小说简单逻辑 一 准备工作 1)安装Python 2)安装PIP 3)安装scrapy 4)安装pywin32 5)安装VCForPython27.exe ........... 具体 ...

  8. 初次尝试python爬虫,爬取小说网站的小说。

    本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...

  9. python3下BeautifulSoup练习一(爬取小说)

    上次写博客还是两个月以前的事,今天闲来无事,决定把以前刚接触python爬虫时的一个想法付诸行动:就是从网站上爬取小说,这样可以省下好多流量(^_^). 因为只是闲暇之余写的,还望各位看官海涵:不足之 ...

随机推荐

  1. Ajax禁止重复提交

    var pendingRequests = []; var generatePendingRequestKey = function (obj) { return obj.data || {}; } ...

  2. 扩展Spring切面功能

    概述 Spring的切面(Spring动态代理)在Spring中应用十分广泛,例如还有事务管理,重试等等.网上介绍SpringAop源码很多,这里假设你对SpringAop有基本的了解.如果你认为Sp ...

  3. 《mysql必知必会》读书笔记--存储过程的使用

    以前对mysql的认识与应用只是停留在增删改查的阶段,最近正好在学习mysql相关内容,看了一本书叫做<MySQL必知必会>,看了之后对MySQL的高级用法有了一定的了解.以下内容只当读书 ...

  4. linux makefile 编译多个.c文件 实例

    本例由 main.c  add.c sub.c add_sub.h 四个文件组成:编写Makefile执行程序 /******************************************* ...

  5. WebService WSDL结构分析

    转载地址:http://blog.csdn.net/sunchaohuang/article/details/3076375      WSDL (Web Services Description L ...

  6. HDU1792A New Change Problem(GCD规律推导)

    A New Change Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. SVN Upgrade working copy

    出现这个的原因是因为你机器上安装的svn客户端版本过高,而你从别的地方拷贝代码中的svn版本过低,点击SVN Upgrade working copy可以将代码中的svn版本升级.

  8. css变量

    CSS变量: 目前主流浏览器都已支持CSS变量,Edge 浏览器也支持 CSS 变量.用户可以方便地在css中使用自定义变量. <!DOCTYPE html> <html lang= ...

  9. RPA(Robotic Process Automation)的概要介绍

    最近因为公司业务的需要,开始关注RPA的内容,奈何国内相关的信息太少,只能硬着头皮啃英文了. 下面记录的内容作为学习笔记,有不对的地方请大家指教. 首先RPA(Robotic Process Auto ...

  10. 关于双11过后MATLAB许可过期问题的解决方案

    在距离双11还有2个月之前,matlab会提示:Your MATLAB license will expire in 50 days --------------------------------- ...