Python 小说内容抓取
2017-10-21 12:30
459 查看
环境 Python27 pycharm2017.2 代码如下: # encoding=utf8 import sys reload(sys) sys.setdefaultencoding('utf-8') import urllib import urllib2 from bs4 import BeautifulSoup if __name__ == '__main__': url = 'http://www.136book.com/huaqiangu/' head = {} head[ 'User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 ' \ '(KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' req = urllib2.Request(url, headers=head) response = urllib2.urlopen(req) html = response.read() soup = BeautifulSoup(html, 'lxml') soup_texts = soup.find('div', id='book_detail', class_='box1').find_next('div') # 打开文件 f = open('D:/huaqianguo.txt', 'w') # 循环解析链接地址 for link in soup_texts.ol.children: if link != '\n': download_url = link.a.get('href') download_req = urllib2.Request(download_url, headers=head) download_response = urllib2.urlopen(download_req) download_html = download_response.read() download_soup = BeautifulSoup(download_html, 'lxml') download_soup_texts = download_soup.find('div', id='content') # 抓取其中文本 download_soup_texts = download_soup_texts.text # 写入章节标题 f.write(link.text + '\n\n') # 写入章节内容 f.write(download_soup_texts) f.write('\n\n') f.close()
相关文章推荐
- Python 小说内容抓取
- Python爬虫抓取小说内容——20180314
- python3 [爬虫入门实战] 爬虫之selenium 模拟QQ登陆抓取好友说说内容(暂留)
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- python 3.X版本 利用urllib.request 通过制定的URL抓取网易内容
- Pyhton实例,抓取百度词条关于Python的内容(一)
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- 使用python抓取小说
- 运用python抓取博客园首页的全部数据,并且定时持续抓取新发布的内容存入mongodb中
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- python 使用 urllib2 抓取网页内容
- Python使用代理抓取网页内容
- Python实现周期性抓取网页内容的方法
- Python 抓取动态网页内容方案详解
- 利用Python和Beautiful Soup抓取网页内容
- Python抓取知乎答案内容
- 程序 python 抓取新浪读书频道小说
- [python]抓取网页的内容
- 第三百四十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容—meta属性返回指定值给回调函数—Scrapy内置图片下载器
- 用Python的Lxml库抓取网页内容