import requests
from lxml import etree
from urllib import parse
import os, time def get_page_html(url):
'''向url发送请求'''
resoponse = session.get(url, headers=headers, timeout=timeout)
try:
if resoponse.status_code == 200:
return resoponse
except Exception:
return None def get_next_url(resoponse):
'''获取下一页的url链接'''
if resoponse:
try:
selector = etree.HTML(resoponse.text)
url = selector.xpath("//a[@id='j_chapterNext']/@href")[0]
next_url = parse.urljoin(resoponse.url, url)
return next_url
except IndexError:
return None def xs_content(resoponse):
'''获取小说的章节名,内容'''
if resoponse:
selector = etree.HTML(resoponse.text)
title = selector.xpath("//h3[@class='j_chapterName']/text()")[0]
content_xpath = selector.xpath(
"//div[contains(@class,'read-content') and contains(@class,'j_readContent')]//p/text()")
return title, content_xpath def write_to_txt(info_tuple: tuple):
if not info_tuple: return
path = os.path.join(BASE_PATH, info_tuple[0])
if not os.path.exists(path):
with open(path + ".txt", "wt", encoding="utf-8") as f:
for line in info_tuple[1]:
f.write(line + "\n")
f.flush() def run(url):
'''启动'''
html = get_page_html(url)
next_url = get_next_url(html)
info_tupe = xs_content(html)
if next_url and info_tupe:
print("正在写入")
write_to_txt(info_tupe)
time.sleep(sleep_time) # 延迟发送请求的时间,减少对服务器的压力。
print("正在爬取%s" % info_tupe[0])
print("正在爬取%s" % next_url)
run(next_url) if __name__ == '__main__':
session = requests.Session()
sleep_time = 5
timeout = 5
BASE_PATH = r"D:\图片\LSZJ" # 存放文件的目录
url = "https://read.qidian.com/chapter/8iw8dkb_ZTxrZK4x-CuJuw2/fWJwrOiObhn4p8iEw--PPw2" # 这是斗破苍穹第一章的url 需要爬取的小说的第一章的链接(url)
headers = {
"Referer": "read.qidian.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
}
print('开始运行爬虫')
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. responsebody和requestbody的使用

    Controller的方法上加了一个@ResponseBody,那么他的作用是什么呢?/** * 新增或修改一条对象. */ @RequestMapping("/save_field&quo ...

  2. Vue2学习笔记:事件对象、事件冒泡、默认行为

    1.事情对象 <!DOCTYPE html> <html> <head> <title></title> <meta charset= ...

  3. Oracle案例12——NBU Oracle恢复

    最近在做NBU ORACLE备份的恢复测试,执行恢复时报错ORA-27211: Failed to load Media Management Library,具体处理过程如下:一.错误信息 执行命令 ...

  4. Oracle案例11——Oracle表空间数据库文件收缩

    我们经常会遇到数据库磁盘空间爆满的问题,或由于归档日志突增.或由于数据文件过多.大导致磁盘使用紧俏.这里主要说的场景是磁盘空间本身很大,但表空间对应的数据文件初始化的时候就直接顶满了磁盘空间,导致经常 ...

  5. virtual memory exhausted: Cannot allocate memory 解决方法

    在阿里云买了个云服务器,内存1G.编译kudu时出现下面的错误: virtual memory exhausted: Cannot allocate memory 问题原因:由于物理内存本身很小,且阿 ...

  6. Hadoop HA 高可用集群的搭建

    hadoop部署服务器 系统 主机名 IP centos6.9 hadoop01 192.168.72.21 centos6.9 hadoop02 192.168.72.22 centos6.9 ha ...

  7. jprofiler9.2注册码

    jprofiler9.2注册码 2016-08-23 18:11 3658人阅读 评论(2) 收藏 举报 L-Larry_Lau@163.com#23874-hrwpdp1sh1wrn#0620 L- ...

  8. Django之Model (ORM)

    传统操作数据库 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层 ...

  9. SGU---102 欧拉函数

    题目链接: https://cn.vjudge.net/problem/SGU-102#author=0 题目大意: 求解小于等于N的且与N互质的数字有多少个 解题思路: 直接求欧拉函数即可 关于欧拉 ...

  10. jdk1.5-jdk1.9的主要区别

    jdk1.5相对以前jdk版本主要新增功能 1.自动拆箱和装箱 其中基本数据类型的包装类有:Double,Float,Long,Integer,Short,Character和Boolean 2.提供 ...