python爬蟲爬取斗破蒼穹小說:
import requests
import time
import re
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.7 Safari/537.36'}
num = 1
def get_info(url):
? ? res=requests.get(url,headers=headers)
? ? if res.status_code==200:
? ? ? ? contents = re.findall('<p>(.*?)</p>',res.content.decode('utf-8'),re.S)
? ? ? ? contents = str(contents).replace('&rdquo','')
? ? ? ? contents = str(contents).replace('&hellip','')
? ? ? ? contents = str(contents).replace('&ldquo','')
? ? ? ? contents = str(contents).replace(''''天才一秒記住本站網(wǎng)站 www.doupoxs.com 中間是<span style="color:blue">斗破 拼音+小說 首字母</span> 連起來就是斗破小說,喜歡我就記住我吧凡怎!''','')
? ? ? ? with open('F:/doupo.txt','a',encoding='utf-8') as f:
? ? ? ? ? ? f.write(contents+'\n')
? ? else:
? ? ? ? pass
if __name__=='__main__':
? ? urls=['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i)) for i in range(2,1665)]
? ? for url in urls:
? ? ? ? get_info(url)
? ? ? ? print(num)
? ? ? ? num += 1
? ? ? ? time.sleep(1)
下載地址: