1. 程式人生 > >python超簡化的18行代碼爬一本小說

python超簡化的18行代碼爬一本小說

位置 nor def agen find kit gbk pytho pla

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)

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

python超簡化的18行代碼爬一本小說