前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python爬取小说

Python爬取小说

作者头像
py3study
发布2020-01-07 14:37:50
1.7K0
发布2020-01-07 14:37:50
举报
文章被收录于专栏:python3python3

这次爬虫并没有遇到什么难题,甚至没有加header和data就直接弄到了盗版网站上的小说,真是大大的幸运。

所用模块:urllib,re

主要分三个步骤:

(1)分析小说网址构成;

(2)获取网页,并分离出小说章节名和章节内容;

(3)写入txt文档。

代码语言:javascript
复制
#-*-coding:GBK-*-
#author:zwg
'''
爬取某小说网站的免费小说
'''
import urllib
import urllib2
import re
url='http://www.bxwx8.org/b/8/8987/2093383.html'
url1='http://www.bxwx8.org/b/8/8987/2093%d.html'
def gethtml(url):
    page=urllib.urlopen(url)
    html=page.read()
    return html
def get_name_content(html):
    re1=re.compile('<title>.+?</title>')
    re2=re.compile('<div id="content"><div id="adright">.+?</div>')
    s1=re1.findall(html)
    s2=re2.findall(html)
    if len(s1)>0 and len(s2)>0:
        name=s1[0].replace('<title>','')
        name=name.replace('TXT下载-笔下文学</title>','')
        content=s2[0].replace('''<div id="content"><div id="adright">''','')
        content=content.replace('</div>','')
        content=content.replace('<br /><br />    ','\n\t')
        content = content.replace('    ', '')
    else:
        name=' '
        content=' '
    return name,content
def write_novel(i,file1):
    file1.writelines('\n\r')
    s=383+i;
    html = gethtml(url1 % s)
    name,content = get_name_content(html)
    file1.writelines(name)
    file1.writelines('\n\t')
    file1.writelines(content)
    file1.writelines('\n\n\n')

file1=file('间客.txt','w+')
[write_novel(i,file1) for i in range(50)]
file1.close()

好了,不说了,我要看小说去了,Bye。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档