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

发布时间:2020-06-12 16:10:42 作者:大大大棉花
来源:网络 阅读:484

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('\n') #为了分行更清楚
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)

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

推荐阅读:
  1. 小白的python爬虫,40代码教你爬取豆瓣小说
  2. 用python“爬”一篇小说

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 爬虫 超简单

上一篇:netty框架和spring框架的区别

下一篇:C语言中"&"怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》