python爬虫得到《三国演义》
2017-03-27 15:29
218 查看
第一天重拾信心开始学习爬虫!这是看了简书一位作者的文章感觉很详细,参照人家的文章自己跑了一遍。
原文链接为:http://www.jianshu.com/p/23070977f63c
首先安装好bs4,
import urllib.request
>>> import chardet
>>> from bs4 import BeautifulSoup
>>> url="http://www.shicimingju.com/book/sanguoyanyi.html"#要爬取的网络地址
>>> menuCode=urllib.request.urlopen(url).read()#将网页源代码赋予menuCode
#使用bs4处理后得到整个页面的soup和要找的部分soup2
>>> soup=BeautifulSoup(menuCode,"html.parser")#使用html解析器进行解析
>>> menu=soup.find_all(id="mulu")#在soup中找到id为mulu的节点
>>> values=",".join(str(v) for v in menu)#将menu转换为str类型
>>> soup2=BeautifulSoup(values,"html.parser");
>>> soup2=soup2.ul#用子节点代替soup2
>>> bookName=soup.h1.string#找到了书名
>>> f=open("bookName.txt","a",encoding="utf8")
#爬取章节url,并解决url为本地的问题
>>> bookMenu=[]#章节list
>>> bookMenuUrl=[]#章节url的list
>>> urlBegin="http://www.shicimingju.com"#解决url为本地的问题
>>> for i in range(0,len(bookMenuUrl)):#依次替换url,读取每章页面的内容
chapterCode=urllib.request.urlopen(urlBegin+bookMenuUrl[i]).read()
result=chardet.detect(chapterCode)#检验读取的页面的编码方式
if(result["confidence"]>0.5):#如果概率大于0.5,采取这种编码方式
chapterCode=chapterCode.decode(result["encoding"])
chapterSoup=BeautifulSoup(chapterCode,"html.parser")#使用BS读取解析网页代码
chapterResult=chapterSoup.find_all(id="con2")#找到id=“con2”的节点,可以点击第一回内容的网站上自己查看
chapterResult=",".join(str(v) for v in chapterResult)
chapterSoup2=BeautifulSoup(chapterResult,"html.parser")
chapterSoup2=chapterSoup2.br
f.write(bookMenu[i])#写入文件的每章标题
for j in range(0,len(chapterSoup2)):#循环写入每章内容
chapterText=chapterSoup2.contents[j].string
f.write(chapterText)
整个爬虫结束后,是一本《三国演义》的书。对于这碗美丽的汤,不需要我们用正则表达式,只用根据节点进行查询就可以了。
原文链接为:http://www.jianshu.com/p/23070977f63c
首先安装好bs4,
import urllib.request
>>> import chardet
>>> from bs4 import BeautifulSoup
>>> url="http://www.shicimingju.com/book/sanguoyanyi.html"#要爬取的网络地址
>>> menuCode=urllib.request.urlopen(url).read()#将网页源代码赋予menuCode
#使用bs4处理后得到整个页面的soup和要找的部分soup2
>>> soup=BeautifulSoup(menuCode,"html.parser")#使用html解析器进行解析
>>> menu=soup.find_all(id="mulu")#在soup中找到id为mulu的节点
>>> values=",".join(str(v) for v in menu)#将menu转换为str类型
>>> soup2=BeautifulSoup(values,"html.parser");
>>> soup2=soup2.ul#用子节点代替soup2
>>> bookName=soup.h1.string#找到了书名
>>> f=open("bookName.txt","a",encoding="utf8")
#爬取章节url,并解决url为本地的问题
>>> bookMenu=[]#章节list
>>> bookMenuUrl=[]#章节url的list
>>> for i in range(1,len(soup2.contents)-1):#依次爬取所有章节 bookMenu.append(soup2.contents[i].string) bookMenuUrl.append(soup2.contents[i].a["href"])依次爬取每章内容并写入txt
>>> urlBegin="http://www.shicimingju.com"#解决url为本地的问题
>>> for i in range(0,len(bookMenuUrl)):#依次替换url,读取每章页面的内容
chapterCode=urllib.request.urlopen(urlBegin+bookMenuUrl[i]).read()
result=chardet.detect(chapterCode)#检验读取的页面的编码方式
if(result["confidence"]>0.5):#如果概率大于0.5,采取这种编码方式
chapterCode=chapterCode.decode(result["encoding"])
chapterSoup=BeautifulSoup(chapterCode,"html.parser")#使用BS读取解析网页代码
chapterResult=chapterSoup.find_all(id="con2")#找到id=“con2”的节点,可以点击第一回内容的网站上自己查看
chapterResult=",".join(str(v) for v in chapterResult)
chapterSoup2=BeautifulSoup(chapterResult,"html.parser")
chapterSoup2=chapterSoup2.br
f.write(bookMenu[i])#写入文件的每章标题
for j in range(0,len(chapterSoup2)):#循环写入每章内容
chapterText=chapterSoup2.contents[j].string
f.write(chapterText)
整个爬虫结束后,是一本《三国演义》的书。对于这碗美丽的汤,不需要我们用正则表达式,只用根据节点进行查询就可以了。
相关文章推荐
- 【Python脚本】-爬虫得到CSDN博客的文章访问量和评论量
- 爬虫Get请求参数匹配得到字典类型格式(Python)
- C代码中如何得到python脚本异常时的traceback信息
- 毕业设计中怎样用python写一个搜索引擎的分布式爬虫---异样的美感
- Python的爬虫程序
- 曲谱搜索进展 及 python的轻量级爬虫框架
- 用Python通过CellID从Google得到经纬度
- Python爬虫
- python文件操作(2)--分析扫描得到的日志文件把文件状态导入Excel表格
- 07-爬虫的多线程调度 | 01.数据抓取 | Python
- 用 python 做简单的网页爬虫程序
- 【用Python写爬虫】获取html的方法【三】:使用cPAMIE
- Getting Started Spidering a Site使用Chilkat(python)练习的一个爬虫(from :http://www.example-code.com)
- python得到汉字拼音首字母
- 【用Python写爬虫】获取html的方法【四】:使用urllib下载文件
- 用 python 做简单的网页爬虫程序
- 【用Python写爬虫】获取html的方法【二】:使用pycurl
- python文件操作(2)--分析扫描得到的日志文件把文件状态导入Excel表格
- python搜索引擎和爬虫框架介绍