Python爬虫-《神雕侠侣》
Python3.5
爬取《神雕侠侣》http://www.kanunu8.com/wuxia/201102/1610.html
武侠迷,所以喜欢爬取武侠小说
#!/usr/bin/python
# -*- coding: utf-8 -*- from selenium import webdriver
import os
from docx import Document
import re class House(): def __init__(self):
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'}
self.baseUrl = 'http://www.kanunu8.com/wuxia/201102/1610.html'
self.basePath = os.path.dirname(__file__) def makedir(self, name):
path = os.path.join(self.basePath, name)
isExist = os.path.exists(path)
if not isExist:
os.makedirs(path)
print('File has been created.')
else:
print('The file is existed.')
#切换到该目录下
os.chdir(path) def connect(self, url):
try:
driver = webdriver.PhantomJS()
driver.get(url)
return driver
except:
print('This page is not existed.') #爬取每个板块中每一章节的链接地址
def getBookLinkList(self, url):
driver = self.connect(url)
bookLinkList = []
try:
#找到所有href链接
bookLinks = driver.find_elements_by_xpath("//a")
for link in bookLinks:
temp = link.get_attribute('href')
print(temp)
try:
#通过正则表达式筛选出各章节的链接
pattern = re.compile(".+\/[0-9]{5}\.html$")
if pattern.match(temp):
print('ok')
bookLinkList.append(link.get_attribute('href'))
except:
print('little error')
except:
print('Error') return bookLinkList #爬取每本书的细节数据
def getBookDetail(self, url):
driver = self.connect(url)
try:
#找到标题和文章内容
title = driver.find_element_by_xpath('//h2').text
content = driver.find_element_by_xpath('//p').text
print(title)
print(content)
except:
print('Error.')
return title, content def getData(self):
doc = Document()
self.makedir('StoryFiles')
bookLinkList = self.getBookLinkList(self.baseUrl)
for linkUrl in bookLinkList:
doc.add_paragraph(self.getBookDetail(linkUrl)) doc.save('神雕侠侣.docx') if __name__ == '__main__':
house = House()
house.getData()
Python爬虫-《神雕侠侣》的更多相关文章
- 爬虫前篇 /https协议原理剖析
爬虫前篇 /https协议原理剖析 目录 爬虫前篇 /https协议原理剖析 1. http协议是不安全的 2. 使用对称秘钥进行数据加密 3. 动态对称秘钥和非对称秘钥 4. CA证书的应用 5. ...
- Python网络爬虫http和https协议
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- Python爬虫-02:HTTPS请求与响应,以及抓包工具Fiddler的使用
目录 1. HTTP和HTTPS 1.1. HTTP的请求和响应流程:打开一个网页的过程 1.2. URL 2. 客户端HTTP请求 3. Fiddler抓包工具的使用 3.1. 工作原理 3.2. ...
- java爬虫爬取https协议的网站时,SSL报错, java.lang.IllegalArgumentException TSLv1.2 报错
目前在广州一家小公司实习,这里的学习环境还是挺好的,今天公司从业十几年的大佬让我检查一下几年前的爬虫程序是否还能使用…… 我从myeclipse上check out了大佬的程序,放到workspace ...
- Python爬虫帮你打包下载所有抖音好听的背景音乐,还不快收藏一起听歌【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- python爬虫相关
一.Python re模块的基本用法: https://blog.csdn.net/chenmozhe22/article/details/80601971 二.爬取网页图片 https://www. ...
- 02.Python网络爬虫第二弹《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- Python网络爬虫第二弹《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- python网络爬虫《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
随机推荐
- Delphi 常用API 函数列表
Delphi 常用API 函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小AnyPopup 判断屏幕上是否存在任何弹出式窗口ArrangeIconicWi ...
- PHP FILTER_SANITIZE_STRIPPED 过滤器
定义和用法 FILTER_SANITIZE_STRIPPED 过滤器去除或编码不需要的字符. 该过滤器是 FILTER_SANITIZE_STRING 过滤器的别名 该过滤器删除那些对应用程序有潜在危 ...
- 2017年上半年,一线城市豪宅TOP50成交均价排名
辣眼睛!最新豪宅排行榜来了!释放重磅信号 东方财富网 2017-07-27 08:47 阅读:152 摘要:在中国,在买房这个问题上,不少工薪族都感到亚历山大.但是,在富豪眼里,犯难的是投资哪个房地产 ...
- artTemplate性能卓越的 js 模板引擎
artTemplate-3.0 新一代 javascript 模板引擎 目录 特性 快速上手 模板语法 下载 方法 NodeJS 使用预编译 更新日志 授权协议 特性 性能卓越,执行速度通常是 Mus ...
- 1.6 USB的插入检测机制
- 前端(十四)—— JavaScript常用类:Number、Date类、字符串、数组、Math类、正则
JS常用类:Number类.Date类.Math类.字符串.数组.正则 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | - ...
- 调用API接口,查询手机号码归属地(1)
使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...
- Apache配置 PHP 支持
1,在服务区安装PHP 解压 php 到纯英文路径目 2,添加 PHP处理模块 LoadModule php7_module C:/ProgramData/php/php7apache2_4.dl 3 ...
- Python 内置函数&filter()&map()&reduce()&sorted()
常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1 ...
- centos yum 安装 tomcat
1.安装 yum install tomcat 2.卸载 yum remove tomcat 3.地址映射 http://localhost:8080/ /usr/share/tomcat/webap ...