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 )服务器传输超文 ...
随机推荐
- 页面background不随滚动条填充颜色
这我又遇到了个问题,这问题我连问都不知道该怎么问,先搁在这儿,如果有办法了,再来补充. 原因:因为颜色板块是100%宽度,和页面保持同宽,所以拖动滚动条,右侧就大于了页面宽度. bug: 我写页面的 ...
- 阿里云物联网边缘计算加载MQTT驱动
写在前面 本文在LinkEdge快速入门样例驱动的基础上,加载了MQTT订阅的客户端,使得边缘端容器可以通过MQTT获得外部数据. 1. 系统需求 物联网边缘计算平台,又名Link IoT Edge[ ...
- 搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
http://blog.chinaunix.net/uid-20639775-id-154497.html
- checklistbox用法
删除:CheckListBox.DeleteSelected; 上下移: CheckListBox.Items.Move 删除用 CheckListBox1.Items.Delete(Index); ...
- HTML5自定义属性的设置与获取
<div id="box" aaa="bbb" data-info="hello"></div> <body& ...
- GDI及Windows的消息机制
什么是GDI? GDI, Graphics Device Interface GDI在以下位置已经被微软列为Legacy Graphics,不建议使用来开发应用程序(http://msdn.micro ...
- 洛谷 P3369 【模板】普通平衡树 (Treap)
题目链接:P3369 [模板]普通平衡树 题意 构造一种数据结构满足给出的 6 种操作. 思路 平衡树 平衡树的模板题. 先学习了一下 Treap. Treap 在插入结点时给该结点随机生成一个额外的 ...
- localStorage使用总结(转载)
localStorage使用总结 本文转载自:https://www.cnblogs.com/st-leslie/p/5617130.html(点击标题可跳转至原文) 一.什么是localStora ...
- react map循环数据 死循环
项目条件:react es6 antidesign 已在commonState中获取到list,但是在循环map填充DOM的时候陷入死循环. 原因:因为是子组件 ,在父组件请求数据的时候 有个时差过程 ...
- js导出复杂表头(多级表头)的excel
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...