本爬虫的特点:

1.目标:静态网站

2.级数:二级

3.线程:单线程(未采用同步,为了避免顺序错乱,因此采用单线程)

4.结果:爬取一部网络小说,将分散的各章节合并成一个txt文本文件

获取网页模板:

def get_url(url):try:response = requests.get(url)print(response.encoding)print(response.apparent_encoding)response.encoding = response.apparent_encodingif response.status_code == 200:return response.textelse:print("url Error:", url)except RequestException:print("URL RequestException Error:", url)return None

解析保存函数:

def parse_url(html):count = 0essay = ""pattern = re.compile('<td class="L"><a href="(.*?)">(.*?)</a></td>', re.S)items = re.findall(pattern, html)pattern_page = re.compile('<meta property="og:url" content="(.*?)"/>', re.S)item_page = re.findall(pattern_page, html)print(items)print(items.__len__())for item in items:count += 1if count <= 2416:continuethis_url = item_page[0] + item[0]this_title = item[1]essay = get_book(this_url, this_title).replace("\ufffd", "*")try:if count % 100 == 1:file = open(sys.path[0]+"凡人修仙传.txt", "a")file.write(essay)if count % 100 == 0 or count == items.__len__():file.close()print("前"+str(count)+"章保存完毕!")print("下载到第 " + str(count) + "章", item, count / items.__len__() * 100, "%")except RequestException:# print("Error", item)print(essay)

完整代码:

import requests
from requests.exceptions import RequestException
import re
import sys
from multiprocessing import Pool
import sqlite3
import osdef get_url(url):try:response = requests.get(url)print(response.encoding)print(response.apparent_encoding)response.encoding = response.apparent_encodingif response.status_code == 200:return response.textelse:print("url Error:", url)except RequestException:print("URL RequestException Error:", url)return Nonedef parse_url(html):count = 0essay = ""pattern = re.compile('<td class="L"><a href="(.*?)">(.*?)</a></td>', re.S)items = re.findall(pattern, html)pattern_page = re.compile('<meta property="og:url" content="(.*?)"/>', re.S)item_page = re.findall(pattern_page, html)print(items)print(items.__len__())for item in items:count += 1if count <= 2416:continuethis_url = item_page[0] + item[0]this_title = item[1]essay = get_book(this_url, this_title).replace("\ufffd", "*")try:if count % 100 == 1:file = open(sys.path[0]+"凡人修仙传.txt", "a")file.write(essay)if count % 100 == 0 or count == items.__len__():file.close()print("前"+str(count)+"章保存完毕!")print("下载到第 " + str(count) + "章", item, count / items.__len__() * 100, "%")except RequestException:# print("Error", item)print(essay)def get_book(url, title):data = "\n" + str(title) + "\n"pattern = re.compile('<dd id="contents">(.*?)</dd>', re.S)essay = re.findall(pattern, get_url(url))essay_str = str(essay[0])data = data + essay_str.replace("&nbsp;", " ").replace("<br />", "\n")return dataif __name__ == '__main__':parse_url(get_url("https://www.x23us.com/html/0/328/"))

【Python】网络爬虫(静态网站)实例相关推荐

  1. python网络爬虫资源库名_Python网络爬虫

    网友NO.524767 Python网络爬虫与信息提取(实例讲解) 课程体系结构: 1.Requests框架:自动爬取HTML页面与自动网络请求提交 2.robots.txt:网络爬虫排除标准 3.B ...

  2. python爬虫实例教程-Python网络爬虫实例教程(视频讲解版)

    第1章 网络爬虫概述 1 1.1 认识网络爬虫 1 1.1.1 网络爬虫的含义 1 1.1.2 网络爬虫的主要类型 2 1.1.3 简单网络爬虫的架构 3 1.1.4 网络爬虫的应用场景 3 1.2 ...

  3. python爬虫教程书籍-Python网络爬虫实例教程(视频讲解版)

    第1章 网络爬虫概述 1 1.1 认识网络爬虫 1 1.1.1 网络爬虫的含义 1 1.1.2 网络爬虫的主要类型 2 1.1.3 简单网络爬虫的架构 3 1.1.4 网络爬虫的应用场景 3 1.2 ...

  4. python爬虫教程-Python网络爬虫实例教程(视频讲解版)

    第1章 网络爬虫概述 1 1.1 认识网络爬虫 1 1.1.1 网络爬虫的含义 1 1.1.2 网络爬虫的主要类型 2 1.1.3 简单网络爬虫的架构 3 1.1.4 网络爬虫的应用场景 3 1.2 ...

  5. python基础实例 韦玮 pdf_精通Python网络爬虫 核心技术、框架与项目实战 作者:韦玮PDF...

    文件目录: 书本介绍: 书名 精通Python网络爬虫:核心技术.框架与项目实战 作者 韦玮著 出版社 机械工业出版社 出版日期 2017 内容简介 本书从系统化的视角,为那些想学习Python网络爬 ...

  6. Python网络爬虫实例1:股票数据定向爬虫

    Python网络爬虫实例:股票数据定向爬虫 一.功能描述 目标:获取上交所和深交所所有股票的名称和交易信息 输出:保存到文件中 技术路线:requests-bs4-re 二.候选数据网站选择 候选网站 ...

  7. 【Python爬虫9】Python网络爬虫实例实战

    文章目录 2.1自动化登录Facebook 2.3自动化登录Linkedin 爬取Google真实的搜索表单 爬取依赖JavaScript的网站Facebook 爬取典型在线商店Gap 爬取拥有地图接 ...

  8. python网络爬虫_Python网络爬虫——爬取视频网站源视频!

    原标题:Python网络爬虫--爬取视频网站源视频! 学习前提 1.了解python基础语法 2.了解re.selenium.BeautifulSoup.os.requests等python第三方库 ...

  9. 嵩天《Python网络爬虫与信息提取》实例2:中国大学排名定向爬虫

    在介绍完requests库和robots协议后,嵩天老师又重点介绍了如何通过BeautifulSoup库进行网页解析和信息提取.这一部分就是在前面内容的基础上,综合运用requests库和Beauti ...

最新文章

  1. Spring Boot 2.3 中开启Spring Security
  2. 三目运算符字符串拼接
  3. ip addr 命令:LOWER_UP字段意义
  4. Sublime Text 3插件之Emmet:HTML/CSS代码快速编写神器
  5. Lucene正则表达式查询RegenxQuery
  6. 干货|MIT线性代数课程精细笔记5
  7. 验证必须是数字php,Element 中表单非必填数据项 必须为数字的验证问题
  8. 【C++】 53_被遗弃的多重继承 (上)
  9. 中心药库管理系统 杀毒软件
  10. 2021年中国原油产量、需求量及石油原油行业发展趋势分析[图]
  11. php 邮箱反垃圾机制,企业邮箱中的反垃圾邮件规则
  12. 深入 javascript 之 call函数 用法
  13. 2021-08-29 UML笔记
  14. 光纤收发器具体是怎么使用的?光纤收发器使用方法详解!
  15. 2021年语音合成年度总结
  16. 爆了!K哥搞定8个Offer
  17. git@gitlab.com: Permission denied报错
  18. react router教程
  19. 11-字符串的格式化
  20. openwrt取消strip 软件包

热门文章

  1. 第一次亲密接触(3)
  2. [Network] 关于ARP病毒
  3. mybatis一对多,多对一映射,collection和association标签的使用
  4. 通胀飙升、加息,投资者需要好的投资标的
  5. linux中人脸识别不了,linux 人脸识别
  6. mobiscroll日期插件
  7. 使用Servlet写一个hello world
  8. 论文投稿指南——收藏|如何选择SCI期刊(含选刊必备神器)
  9. 树莓 php mysql,树莓搭建Nginx+PHP7.0+mysql服务器
  10. 史玉柱高调杀回IT产业 2亿元豪赌网游征途