刚学,使用python3,安装了BeautifulSoup,一个Python library,解析html
路径:
http://www.crummy.com/software/BeautifulSoup/
from html.parser import HTMLParser
from bs4 import BeautifulSoup as bs
import urllib.request
import re
def parsechapter(url, out):
data = urllib.request.urlopen(url)
dataDecode = data.read().decode('utf-8')
data.close()
soup = bs(dataDecode)
for content in soup.findAll(id="chapterContent"):
for nc in content.findAll("p"):
print(nc.span.previousSibling, file=out)
try
a_file = open("test.txt", mode="a", encoding="utf-8")
showchapter_url = 'http://book.zongheng.com/showchapter/189169.html'
chapterData = urllib.request.urlopen(showchapter_url)
chapterDataDecode = chapterData.read().decode('utf-8')
chapterData.close()
chapterDataSoup = bs(chapterDataDecode)
for chapters in chapterDataSoup.findAll("div", attrs={'class':"booklist"}):
for chapter in chapters.findAll("a"):
print(chapter.get_text(), file=a_file)
parsechapter(chapter['href'], a_file)
except IOError:
print('file error!')
finally:
if 'a_file' in locals():
a_file.close()
分享到:
相关推荐
Python爬虫教程,采用BeautifulSoup技术,爬取相关的网址,摘取小说,然后保存到本地存储。同类方法还有Scrapy等等,这里做了一个简单的示范 爬取某著名小说网站的一章资源,然后存储到本地的MySQL库中,
不到200行Python代码爬个小说网站源码
python,爬虫代码,可以抓取挺多,像小说啥的,可以用于平时python课的作业,或者自己用
另外还有专门的爬虫抓取页面,用于爬虫部分免费合规的小说网站的电子书内容 本项目提供的爬虫源代码仅用学习,请勿用于商业盈利。 2.用户使用本系统从事任何违法违规的事情,一切后果由用户自行承担,作者不承担...
抓取豆瓣影评评分 正常的抓取 分析请求的url https://movie.douban.com/subject/26322642/comments?start=20&limit=20&sort=new_score&status=P&percent_type= 里面有用的也就是start和limit参数,我尝试过修改...
飞卢小说网pyhon3爬虫爬取练习附件源码,练习写的不是很完善,欢迎交流
首先,咱先看下爬虫的定义:网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。一句话概括就是网上信息搬运工。 我们...