学习进度-10 python爬虫
学习爬虫的第一个案例是小说爬虫。
小说爬虫首先是解析小说页面源代码,在页面源代码中可以看到小说每章节的内容链接
爬虫的代码:
import requests
import re url = 'http://www.92kshu.cc/69509/'
response = requests.get(url)
response.encoding = 'gbk'
html = response.text
title = re.findall(r'<meta property="og:novel:book_name" content="(.*?)"/>', html)[0]
fb = open('%s.txt' % title, 'w', encoding='utf-8')
# 获取每章的内容
# print(html)
dl = re.findall(r'<dl><dt><i class="icon"></i>正文</dt>(.*?)</dl>', html)[0]
print(dl)
chapter_info_list = re.findall(r'<dd><a href="(.*?)">(.*?)</a></dd>', dl)
print(chapter_info_list)
for chapter_info in chapter_info_list:
chapter_url, chapter_title = chapter_info
chapter_url = "http://www.92kshu.cc%s" % chapter_url
# print(chapter_url)
chapter_response = requests.get(chapter_url)
chapter_response.encoding = 'gbk'
chapter_html = chapter_response.text
chapter_content = re.findall(r'<div class="chapter">(.*?)><br>', chapter_html)[0]
# print(chapter_content)
chapter_content = chapter_content.replace('<p>', '')
chapter_content = chapter_content.replace('</p>', '')
fb.write(chapter_title)
fb.write(chapter_content)
fb.write('\n')
print(chapter_url)
爬虫结果:




学习进度-10 python爬虫的更多相关文章
- 学习进度-16 python爬虫
爬虫是一个程序,这个程序的目的就是为了抓取万维网信息资源,比如你日常使用的谷歌等搜索引擎,搜索结果就全都依赖爬虫来定时获取 从百度可以看出来 爬虫与python关系很紧密, 爬虫的目标对象也很丰富,不 ...
- 学习笔记之Python爬虫
Python 爬虫介绍 | 菜鸟教程 http://www.runoob.com/w3cnote/python-spider-intro.html https://blog.csdn.net/sina ...
- Python学习:10.Python装饰器讲解(一)
情景介绍 一天,在你正在努力加班的时候,老板给交给你了一个任务,就是在这段代码里将所有函数开始输出一个‘hello’最后输出当前时间,再输出一个“end”,这段代码里包含了大量的函数,你会怎么做? d ...
- 学习笔记10—Python 绘图集
ordered_data = np.load('ordered_data_just_TD_mae.npy')results = pd.Series(np.squeeze(np.load('result ...
- 吴裕雄--天生自然python学习笔记:python爬虫PM2.5 实时监测显示器
PM2.5 对人体的健康影响很大,所以空气中的 PM2.5 实时信息受到越来越多的关注. Python 的 Pandas 套件不但可以自动读取网页中的表格 数据 , 还可对数据进行修改.排序等处理,也 ...
- 吴裕雄--天生自然python学习笔记:python爬虫与网页分析
我们所抓取的网页源代码一般都是 HTML 格式的文件,只要研究明白 HTML 中 的标签( Tag )结构,就很容易进行解析并取得所需数据 . HTML 网页结构 HTML 网 页是由许多标签( Ta ...
- 【Python爬虫】入门知识
爬虫基本知识 这阵子需要用爬虫做点事情,于是系统的学习了一下python爬虫,觉得还挺有意思的,比我想象中的能干更多的事情,这里记录下学习的经历. 网上有关爬虫的资料特别多,写的都挺复杂的,我这里不打 ...
- python爬虫小实例
1.python爬取贴吧壁纸 1.1.获取整个页面数据 #coding=utf-8 import urllib def getHtml(url): page = urllib.urlopen(url) ...
- 【学习笔记】PYTHON网络爬虫与信息提取(北理工 嵩天)
学习目的:掌握定向网络数据爬取和网页解析的基本能力the Website is the API- 1 python ide 文本ide:IDLE,Sublime Text集成ide:Pychar ...
随机推荐
- FTP-Publisher Plugin
使用 FTP-Publisher Plugin 在 http://wiki.hudson-ci.org/display/HUDSON/FTP-Publisher+Plugin 有详细说明, 需要注意的 ...
- Pycharm创建一个Django项目
1.创建项目 如果本地没有安装与所选python版本对应Django版本,pycharm会自动下载相应的版本: 创建后运行项目,默认页面为http://127.0.0.1:8000/,打开后: 出现上 ...
- [蓝桥杯2016初赛]卡片换位 BFS
题目描述 你玩过华容道的游戏吗?这是个类似的,但更简单的游戏.看下面 3 x 2 的格子 +---+---+---+ | A | * | * | +---+---+---+ | B | | * | + ...
- 【原】openresty学习
参考文档: 1.openresty最佳实践:https://moonbingbing.gitbooks.io/openresty-best-practices/content/ 2.openResty ...
- Educational Codeforces Round 73 (Rated for Div. 2)D(DP,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[300007],b[3 ...
- Set和Map集合的比较
HashSet:数据进行hashCode比较,然后进行equals方法比较,根据比较结果进行排序.如果要对对象进行排序,对象类要重写hashCode和equals方法.TreeSet:如果要对对象进 ...
- android悬浮按钮(Floating action button)的两种实现方法
原文: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1028/1857.html 最近android中有很多新的设计规范被引入 ...
- Color Space 和 Color Range
颜色有两个属性Color Range和Color Space 有关Color Space的解释可以看下面两个链接: https://www.jianshu.com/p/facdbab5ac20 htt ...
- 浏览器输入URL后HTTP请求返回的完整过程
图:
- Codeforces 1300E. Water Balance
给你一个数列,有一个操作,将一段数字变成其和除以个数,求字典序最小的那一个,分析知,求字典序最小,就是求一个不下降序列,但我们此时有可以更改数字的操作,已知已经不下降的序列不会因为操作而变的更小,只有 ...