恩,写在最前面。肯定是不能抓取VIP章节的,因为这违法了,(其实是我不会)
今天朋友告诉我,他看小说的时候,因为成片成片的错别字,经常是看着看着就看不下去,问我有啥办法解决
我说,你去正版网站看不就没事了......
他说,你不是会爬虫吗,你帮我爬下来......
我说,就我这菜*水平,普通章节吧......
以下是代码,不足之处还请各位大神们,予以指正!
万分感谢!
1 import re 2 import urllib2 3 import time 4 5 def spiders_Qidian(url): 6 header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'} 7 request=urllib2.Request(url,headers=header) 8 page_code=urllib2.urlopen(request).read().decode('utf-8') 9 find='<li data-rid=".*?"><a href="(.*?)" target=".*?" data-eid=".*?" data-cid=".*?" title=".*?">(.*?)</a>.*?</li>' 10 context = re.findall(find,page_code) 11 # 加个标题吧 12 find2 = '<h1><em>(.*?)</em><span><a class=".*?" href=".*?" target=".*?" data-eid=".*?">.*?</a> .*?</span></h1>' 13 context2 = re.findall(find2, page_code) 14 for x in context[0:]: 15 # 跳过所有VIP章节,别问我怎么弄,我也不会.....汗! 16 if 'vipreader' not in x[0]: 17 url='http:'+x[0] 18 try: 19 page_code=urllib2.urlopen(url,timeout=5).read().decode('utf-8') 20 find_code='<div class="read-content j_readContent">(.*?)</div>' 21 context = re.findall(find_code, page_code,re.S) 22 except Exception,e: 23 # 因为偶尔会出现个别章节空白之类的问题 所以在此跳过 24 print str(x[1].replace(' ','').encode('utf-8'))+'【抓取失败】' 25 continue 26 with open(context2[0]+'.txt','a+') as file: 27 file.write('\n') 28 # 去除偶尔的一些空格(其实没啥用) 29 file.write(x[1].replace(' ','').encode('utf-8')) 30 file.write('\n') 31 try: 32 # 如果这个章节里面是空白,就会出现列表指针问题,恩,跳过 33 file.write(context[0].replace(' ','').replace('<p>','\n').encode('utf-8')) 34 except Exception,e: 35 print str(x[1].replace(' ','').encode('utf-8'))+'【写入失败】' 36 file.write('\n') 37 continue 38 print (x[1].replace(' ','').encode('utf-8')) 39 file.close() 40 # 让他稍微休息会,恩0.5秒, 41 time.sleep(0.5) 42 43 44 if __name__ == '__main__': 45 # 这里是起点小说的任意一本小说的目录,对,你没看错 这里必须先点目录,然后再把网址输入进来 46 # 如果你输入网址 回车后发现怎么跳转到网页了,恩,粘贴上网址后加上个空格没事的.....(别打我) 47 url = raw_input('请输入你想要当前小说的目录页地址(必须是目录页哦)') 48 spiders_Qidian(url)
你问我 目录页是 什么鬼?
就是 进入某一部小说的页面后,点击它的目录......(我错了,是我没说清)
就像这样:
转载于:https://www.cnblogs.com/HapyyHao1314/p/7429012.html