1. import requests
  2. from lxml import etree
  3. from urllib import parse
  4. import os, time
  5.  
  6. def get_page_html(url):
  7. '''向url发送请求'''
  8. resoponse = session.get(url, headers=headers, timeout=timeout)
  9. try:
  10. if resoponse.status_code == 200:
  11. return resoponse
  12. except Exception:
  13. return None
  14.  
  15. def get_next_url(resoponse):
  16. '''获取下一页的url链接'''
  17. if resoponse:
  18. try:
  19. selector = etree.HTML(resoponse.text)
  20. url = selector.xpath("//a[@id='j_chapterNext']/@href")[0]
  21. next_url = parse.urljoin(resoponse.url, url)
  22. return next_url
  23. except IndexError:
  24. return None
  25.  
  26. def xs_content(resoponse):
  27. '''获取小说的章节名,内容'''
  28. if resoponse:
  29. selector = etree.HTML(resoponse.text)
  30. title = selector.xpath("//h3[@class='j_chapterName']/text()")[0]
  31. content_xpath = selector.xpath(
  32. "//div[contains(@class,'read-content') and contains(@class,'j_readContent')]//p/text()")
  33. return title, content_xpath
  34.  
  35. def write_to_txt(info_tuple: tuple):
  36. if not info_tuple: return
  37. path = os.path.join(BASE_PATH, info_tuple[0])
  38. if not os.path.exists(path):
  39. with open(path + ".txt", "wt", encoding="utf-8") as f:
  40. for line in info_tuple[1]:
  41. f.write(line + "\n")
  42. f.flush()
  43.  
  44. def run(url):
  45. '''启动'''
  46. html = get_page_html(url)
  47. next_url = get_next_url(html)
  48. info_tupe = xs_content(html)
  49. if next_url and info_tupe:
  50. print("正在写入")
  51. write_to_txt(info_tupe)
  52. time.sleep(sleep_time) # 延迟发送请求的时间,减少对服务器的压力。
  53. print("正在爬取%s" % info_tupe[0])
  54. print("正在爬取%s" % next_url)
  55. run(next_url)
  56.  
  57. if __name__ == '__main__':
  58. session = requests.Session()
  59. sleep_time = 5
  60. timeout = 5
  61. BASE_PATH = r"D:\图片\LSZJ" # 存放文件的目录
  62. url = "https://read.qidian.com/chapter/8iw8dkb_ZTxrZK4x-CuJuw2/fWJwrOiObhn4p8iEw--PPw2" # 这是斗破苍穹第一章的url 需要爬取的小说的第一章的链接(url)
  63. headers = {
  64. "Referer": "read.qidian.com",
  65. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
  66. }
  67. print('开始运行爬虫')
  68. run(url)

python3爬虫-使用requests爬取起点小说的更多相关文章

  1. python3爬虫-通过requests爬取图虫网

    import requests from fake_useragent import UserAgent from requests.exceptions import Timeout from ur ...

  2. python3爬虫-通过requests爬取西刺代理

    import requests from fake_useragent import UserAgent from lxml import etree from urllib.parse import ...

  3. Python3爬虫使用requests爬取lol英雄皮肤

    本人博客:https://xiaoxiablogs.top 此次爬取lol英雄皮肤一共有两个版本,分别是多线程版本和非多线程版本. 多线程版本 # !/usr/bin/env python # -*- ...

  4. 使用scrapy爬虫,爬取起点小说网的案例

    爬取的页面为https://book.qidian.com/info/1010734492#Catalog 爬取的小说为凡人修仙之仙界篇,这边小说很不错. 正文的章节如下图所示 其中下面的章节为加密部 ...

  5. python从爬虫基础到爬取网络小说实例

    一.爬虫基础 1.1 requests类 1.1.1 request的7个方法 requests.request() 实例化一个对象,拥有以下方法 requests.get(url, *args) r ...

  6. python3 爬虫教学之爬取链家二手房(最下面源码) //以更新源码

    前言 作为一只小白,刚进入Python爬虫领域,今天尝试一下爬取链家的二手房,之前已经爬取了房天下的了,看看链家有什么不同,马上开始. 一.分析观察爬取网站结构 这里以广州链家二手房为例:http:/ ...

  7. 【Python3爬虫】我爬取了七万条弹幕,看看RNG和SKT打得怎么样

    一.写在前面 直播行业已经火热几年了,几个大平台也有了各自独特的“弹幕文化”,不过现在很多平台直播比赛时的弹幕都基本没法看的,主要是因为网络上的喷子还是挺多的,尤其是在观看比赛的时候,很多弹幕不是喷选 ...

  8. python3 [爬虫实战] selenium 爬取安居客

    我们爬取的网站:https://www.anjuke.com/sy-city.html 获取的内容:包括地区名,地区链接: 安居客详情 一开始直接用requests库进行网站的爬取,会访问不到数据的, ...

  9. 【Python3 爬虫】14_爬取淘宝上的手机图片

    现在我们想要使用爬虫爬取淘宝上的手机图片,那么该如何爬取呢?该做些什么准备工作呢? 首先,我们需要分析网页,先看看网页有哪些规律 打开淘宝网站http://www.taobao.com/ 我们可以看到 ...

随机推荐

  1. Mongodb的入门(1)window安装mongodb

    mongodb: Mongodb,分布式文档存储数据库,由C++语言编写,旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数 ...

  2. 《Think in JAVA》之每日一读(initianlize)——2013/11/12、13

    了解包括继承在内的初始化全过程,以对所发生的的一切有一个全局的把握,是很有益的. 请看下例: package initialize; class Insect { private int i = 9; ...

  3. 第六章 函数、谓词、CASE表达式 6-2 谓词

    一.什么是谓词 需要满足返回值为真值的函数.谓词的返回值全都是真值(TRUE/ FALSE/UNKNOWN) 如:LIKE/BETWEEN /IS NULL/IS NOT NULL/IN/EXISTS ...

  4. poj_3628 Bookshelf 2

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  5. 能力成熟度模型(CMM)

    能力等级 特点 关键过程 第一级 基本级 软件过程是混乱无序的,对过程几乎没有定义,成功依靠的是个人的才能和经验,管理方式属于反应式   第二级 重复级 建立了基本的项目管理来跟踪进度.费用和功能特征 ...

  6. 转载:eclipse 搭建SSH项目(第二篇,有具体的项目例子)

    原文地址:http://blog.csdn.net/yeohcooller/article/details/9316923 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创 ...

  7. Loadrunner11代理录制&各个常见功能介绍

    1.代理录制: Lr代理工具:C:\Program Files (x86)\HP\LoadRunner\bin\wplus_init_wsock.exe 1) 设置代理 配置代理信息: 2)设置浏览器 ...

  8. Linux系统优化实现高并发

    ulimit -SHn 65535内核优化net.ipv4.ip_forward = 1            #开启路由功能net.ipv4.conf.default.rp_filter = 1   ...

  9. 17级-车辆工程-周金霖 计算机作业 MP4音乐网站

  10. .Net开发八年,坐标杭州,上个月换工作感觉现在.Net岗位很少,希望和同在杭州的同行们交流一下

    .Net开发八年,坐标杭州,中间做过2年Java, 目前新入职一家做防伪溯源的中型公司,200人左右, 之前在一家500人规模的软件公司工作过4年,后面2年工作过3家互联网创业公司, 上个月换工作感觉 ...