亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python爬取小說

系統(tǒng) 1972 0

起因:有一天突然想看本小說,在小說網(wǎng)站上不能下載,廣告太多,便。。。
思路:
–分析網(wǎng)站的結(jié)構(gòu)
–爬取目錄(獲得章節(jié)名和鏈接)
–多線程加載章節(jié)網(wǎng)頁
–正則匹配內(nèi)容
–保存生成錯(cuò)誤日志
–鏈接單個(gè)章節(jié)txt合并為一個(gè)

            
              # -*- coding: utf-8 -*-
"""
Created on Tue Jul  2 18:23:49 2019

@author: 24709
"""
import urllib
import urllib.request
import multiprocessing
from bs4 import BeautifulSoup
import re
import os
import time
#小說首頁http://m.wenxuedu.com/html/208249
dirPath = "C:/Users/24709/Desktop/txtxt/"
#所有txt位于的文件夾路徑(注意\與/的區(qū)別),最終文件為output
soup=""
titles=[] #存取所有章節(jié)名稱
urls=[] #存取所有章節(jié)的URL鏈接


################從目錄頁面爬取章節(jié)名稱和url地址##################################
def geturl():
    print('正在加載章節(jié).....')
    for index in range(5,6):#(5,151)爬取第5到第150頁目錄
        try:
            request = urllib.request.Request("http://m.wenxuedu.com/html/208249_"+str(index))
            response = urllib.request.urlopen(request,timeout=8)
            content = response.read()
            data = content.decode('utf-8')
            # soup轉(zhuǎn)換
            soup = BeautifulSoup(data, "html.parser")
            i=0;
            for link in soup.findAll('li'):
                #獲取 link 的  href 屬性內(nèi)容
                if re.search(r'章',str(link.a.string)) :
                    if i>4:
                        print(str(link.a.string))
                        #print(link.a.get('href'))
                        titles.append(str(link.a.string))
                        urls.append(str(link.a.get('href')))
                    i=i+1
        except:
            try:
                #再次嘗試
                request = urllib.request.Request("http://m.wenxuedu.com/html/208249_"+str(index))
                response = urllib.request.urlopen(request,timeout=8)
                content = response.read()
                data = content.decode('utf-8')
                # soup轉(zhuǎn)換
                soup = BeautifulSoup(data, "html.parser")
                i=0;
                for link in soup.findAll('li'):
                    #獲取 link 的  href 屬性內(nèi)容
                    if re.search(r'章',str(link.a.string)) :
                        if i>4:
                            print(str(link.a.string))
                            #print(link.a.get('href'))
                            titles.append(str(link.a.string))
                            urls.append(str(link.a.get('href')))
                        i=i+1
            except:
                #目錄下載失敗將索引寫入錯(cuò)誤日志
                writefile('error_log',"index:{} \n".format(index))
###########################根據(jù)url下載小說內(nèi)容##################################
def getcontent(url):
    request = urllib.request.Request("http://m.wenxuedu.com"+url)
    response = urllib.request.urlopen(request)
    content = response.read()
    data = content.decode('utf-8')
    # soup轉(zhuǎn)換
    soup = BeautifulSoup(data, "html.parser")
    a=str(soup.find(id='novelcontent').p).replace('
              
','')[3:-4] try: #嘗試刪去冗余的頭(標(biāo)題)和尾 TEMP=int(re.search(str(r"[)]"),a).span()[0])+1 a=a[TEMP:-1*(re.search(str('\u3000\u3000'),a[::-1]).span()[0]+2)] except: pass #print(a) #print("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\") try: #嘗試由沒有第二章 request = urllib.request.Request("http://m.wenxuedu.com"+url[:-1]+"_2") response = urllib.request.urlopen(request) content = response.read() data = content.decode('utf-8') # soup轉(zhuǎn)換 soup = BeautifulSoup(data, "html.parser") b=str(soup.find(id='novelcontent').p).replace('
','')[3:-4] #print(b) try: #嘗試刪去冗余的頭(標(biāo)題) TEMP=int(re.search(str("[)]"),b).span()[0])+1 c=a+b[TEMP:-1] except: c=a+b except: #print("本章無第二章節(jié)") pass return c ############################寫入文件########################################### def writefile(title,content): with open(dirPath+title+".txt",'a',encoding='utf-8') as f: f.write(content) f.close() #######################嘗試下載,下載失敗保存到日志############################## def download(title_url): try: writefile(title_url[0],getcontent(title_url[1])) except: writefile('error_log',"title:{} url:{} \n".format(title_url[0],title_url[1])) ##########################合并txt############################################## def linkTheBook(): print("-------------------開始合成txt-------------------") start0 = time.time() file0 = os.listdir(dirPath) files=[] for file in file0: if re.search(r'(\d+)',file): files.append(file) ##過濾名字里不帶數(shù)字章節(jié)的 files.sort(key=lambda i:int(re.search(r'(\d+)',i)[0]))#用正則提取章節(jié)數(shù)字并排序 res = "" i = 0 for file in files: if file.endswith(".txt"): i += 1 title = "%s" % (file[0:len(file)-4]) with open(dirPath + file, "r", encoding='utf-8') as file: content = file.read() file.close() append = "\n%s\n\n%s" % (title, content) res += append with open(dirPath+"outfile.txt", "w", encoding='utf-8') as outFile: outFile.write(res) outFile.close() end0=time.time() print("-------------------txt合成完成-------------------") print("全書共"+str(len(files))+"章,共"+str(len(res))+"字") print('運(yùn)行時(shí)間 %0.2f s.' % (end0 - start0)) #######################################################################3 if __name__=="__main__": start = time.time() geturl() #爬取目錄中的章節(jié)名稱和url地址到【titles】和【urls】 print("-------------------開始下載-------------------") p = [] print('主程序的PID:%s' % os.getpid()) for [title,url] in zip(titles,urls): p.append(multiprocessing.Process(target=download, args=([title,url],))) #多進(jìn)程同時(shí)下載不同的章節(jié) print("等待所有的進(jìn)程加載完成........") for i in p: i.start() for i in p: i.join() end = time.time() print("-------------------全部下載完成-------------------") print('運(yùn)行時(shí)間 %0.2f s.' % (end - start)) ###################################3 #linkTheBook() pass

用于自我學(xué)習(xí)記錄,歡迎交流指正。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 日韩中文字幕视频 | 四虎4w4w| 波多野结衣免费一区二区三区香蕉 | 网曝门精品国产事件在线观看 | 成人精品视频在线 | 性生大片一级毛片免费观看 | 久久99九九99九九精品 | 免费的黄色网 | 草免费视频 | 亚洲看黄 | 看一级特黄a大片日本片黑人 | 国产成人午夜片在线观看 | 在线播放国产福利视频 | 香蕉超级碰碰碰97视频蜜芽 | 老湿机永久体验 | 精品国产综合区久久久久久 | 91热视频在线 | 国产精品美女久久久久网 | 成人毛片免费观看视频大全 | 香蕉网伊在线中文慕大全 | 四虎国产精品成人永久免费影视 | 一级毛片在线看 | 免费视频爰爱太爽了 | 青青青爽线在线视频观看 | 日本一级一片免费 | 日韩在线欧美在线 | 日本免费的一级v一片 | 国产成人无精品久久久 | 国产欧美成人一区二区三区 | 久久精品免费视频观看 | 97视频免费公开成人福利 | 全黄h全肉边做边吃奶在线观看 | 国产成人精品一区二区免费 | 中文字幕日韩精品在线 | 97精品国产高清在线看入口 | 日韩欧美视频免费观看 | 亚洲欧美色图片 | 久久国产这里只精品免费 | 久久久99精品免费观看精品 | 综合色网站 | 欧美日韩亚洲综合久久久 |