python超简化的18行代码爬一本小说

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python超简化的18行代码爬一本小说相关的知识,希望对你有一定的参考价值。

import urllib.request
import re
def getnvvel():
html = urllib.request.urlopen("http://www.quanshuwang.com/book/44/44683").read().decode(‘gbk‘) # download sould code
urls = re.findall(r‘<li><a href="(.?)" title=".?">(.?)</a></li>‘, html) # regular expression
title = "douluo" # Normoally,you should use request.urlopen
f = open(‘../novel/%s.txt‘ % title, ‘w‘) # create a douluo.txt
for url in urls:
chapter_url = url[0]
chapter_title = url[1]
chapter_content_list = urllib.request.urlopen(chapter_url).read().decode("gbk")
chapter_content_list = re.findall(r‘</script>    .
?<br />(.*?)<script type="text/javascript">‘, chapter_content_list, re.S)
for chapter_content in chapter_content_list:
chapter_content = chapter_content.replace("    ", "")
chapter_content = chapter_content.replace("<br />", "")
f.write(chapter_title) # type chapter_title in douluo.txt
f.write(chapter_content) # type chapter_content in douluo.txt
f.write(‘ ‘) #为了分行更清楚
getnvvel()

如果你想你的代码不容易被发现你可以加上一个header比如

headers = {‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36‘}

html = request.urlopen(url, headers=headers)

当然为了和谐你也可以

import time

在后面某个位置加上下载的位置加上一个

time.sleep(1)

当然,想要加上一些其他防爬虫的东西你就得自己再努力深造了

以上是关于python超简化的18行代码爬一本小说的主要内容,如果未能解决你的问题,请参考以下文章

Python爬取整本小说

10分钟入门爬虫-小说网站爬取

利用python3爬虫爬取漫画岛-非人哉漫画

Python爬虫爬取目标小说并保存到本地

python 爬qidian小说

20194302实验四 Python综合实践