本文以搜書網(wǎng)一小說為例《噓,梁上有王妃!》
目錄網(wǎng)址:https://www.soshuw.com/XuLiangShangYouWangFei/
加載需要的包:
import re from bs4 import BeautifulSoup as ds import requests
獲取小說目錄文件,返回Response [200]>,表示可正常爬取該網(wǎng)頁(yè)
base_url='https://www.soshuw.com/XuLiangShangYouWangFei/' chapter_html=requests.get(base_url) print(chapter_html)
解析目錄網(wǎng)頁(yè) , 輸出結(jié)果為目錄網(wǎng)頁(yè)的源代碼
chapter_page_html=ds(chapter_page,'lxml') print(chapter_page)
打開目錄網(wǎng)頁(yè),發(fā)現(xiàn)在正文的目錄前面有一個(gè)最新章節(jié)目錄(這里有九個(gè)章節(jié)),再完整的目錄中是包含最新章節(jié)的,所以這里最新章節(jié)是不需要的。
在網(wǎng)頁(yè)單擊右鍵選擇“檢查”(或者“屬性”,不同的瀏覽器的叫法不一致,我用的是IE)選擇“元素”列,鼠標(biāo)再右側(cè)代碼塊上移動(dòng)時(shí)。左側(cè)網(wǎng)頁(yè)會(huì)高亮顯示其對(duì)應(yīng)網(wǎng)頁(yè)區(qū)域,找到完整目錄對(duì)應(yīng)的代碼塊。如下圖:
完整目錄的錨有兩個(gè),分別是class="novel_list"和id=“novel108799”,仔細(xì)觀察后發(fā)現(xiàn)class不唯一,所以我們選用id提取該塊內(nèi)容
將完整目錄塊提取出來
chapter_novel=chapter_page.find(id="novel108799") print(chapter_novel)
結(jié)果如下(僅部分結(jié)果):
對(duì)比小說章節(jié)內(nèi)容網(wǎng)址和目錄網(wǎng)址(base_url)發(fā)現(xiàn),我們只需要將base_url和章節(jié)內(nèi)容網(wǎng)址的后半段拼接到一起就可以得到完整的章節(jié)內(nèi)容網(wǎng)址
利用正則語(yǔ)言庫(kù)將地址后半段提取出來
chapter_novel_str=str(chapter_novel) regx = 'dd>a href="/XuLiangShangYouWangFei(.*?)"' chapter_href_list = re.findall(regx, chapter_novel_str) print(chapter_href_list)
拼接url:
定義一個(gè)列表chapter_url_list接收完整地址
chapter_url_list = [] for i in chapter_href_list: url=base_url+i chapter_url_list.append(url) print(chapter_url_list)
打開章節(jié),右鍵→“屬性”,查看內(nèi)容結(jié)構(gòu),發(fā)現(xiàn)小說正文有class和id兩個(gè)錨,class是不變的,id隨著章節(jié)而變化,所以我們用class提取正文
提取正文段
chapter_novel=chapter_page.find(id="novel108799") print(chapter_novel)
提取正文文本和標(biāo)題
body_html=requests.get('https://www.soshuw.com/XuLiangShangYouWangFei/3647144.html') body_page=ds(body_html.content,'lxml') body = body_page.find(class_='content') body_content=str(body) print(body_content) body_regx='br/> (.*?)\n' content_list=re.findall(body_regx,body_content) print(content_list) title_regx = 'h1>(.*?)/h1>' title = re.findall(title_regx, body_html.text) print(title)
with open('1.txt', 'a+') as f: f.write('\n\n') f.write(title[0] + '\n') f.write('\n\n') for e in content_list: f.write(e + '\n') print('{} 爬取完畢'.format(title[0]))
import re from bs4 import BeautifulSoup as ds import requests base_url='https://www.soshuw.com/XuLiangShangYouWangFei' chapter_html=requests.get(base_url) chapter_page=ds(chapter_html.content,'lxml') chapter_novel=chapter_page.find(id="novel108799") #print(chapter_novel) chapter_novel_str=str(chapter_novel) regx = 'dd>a href="/XuLiangShangYouWangFei(.*?)"' chapter_href_list = re.findall(regx, chapter_novel_str) #print(chapter_href_list) chapter_url_list = [] for i in chapter_href_list: url=base_url+i chapter_url_list.append(url) #print(chapter_url_list) for u in chapter_url_list: body_html=requests.get(u) body_page=ds(body_html.content,'lxml') body = body_page.find(class_='content') body_content=str(body) # print(body_content) body_regx='br/> (.*?)\n' content_list=re.findall(body_regx,body_content) #print(content_list) title_regx = 'h1>(.*?)/h1>' title = re.findall(title_regx, body_html.text) #print(title) with open('1.txt', 'a+') as f: f.write('\n\n') f.write(title[0] + '\n') f.write('\n\n') for e in content_list: f.write(e + '\n') print('{} 爬取完畢'.format(title[0]))
到此這篇關(guān)于使用PyCharm批量爬取小說的文章就介紹到這了,更多相關(guān)PyCharm批量爬取小說內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:白城 安慶 天水 西安 隨州 股票 錦州 日照
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用PyCharm批量爬取小說的完整代碼》,本文關(guān)鍵詞 使用,PyCharm,批量,爬取,小,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。