主頁 > 知識庫 > 使用PyCharm批量爬取小說的完整代碼

使用PyCharm批量爬取小說的完整代碼

熱門標(biāo)簽:硅谷的囚徒呼叫中心 企業(yè)做大做強 語音系統(tǒng) Win7旗艦版 百度AI接口 電話運營中心 呼叫中心市場需求 客戶服務(wù)

使用pycharm批量爬取小說

爬取小說的思路:

 1.獲取小說地址

本文以搜書網(wǎng)一小說為例《噓,梁上有王妃!》
目錄網(wǎng)址:https://www.soshuw.com/XuLiangShangYouWangFei/
加載需要的包:

import re
from bs4 import BeautifulSoup as ds
import requests

獲取小說目錄文件,返回Response [200]>,表示可正常爬取該網(wǎng)頁

base_url='https://www.soshuw.com/XuLiangShangYouWangFei/'
chapter_html=requests.get(base_url)
print(chapter_html)

2.分析小說地址結(jié)構(gòu)

解析目錄網(wǎng)頁 , 輸出結(jié)果為目錄網(wǎng)頁的源代碼

chapter_page_html=ds(chapter_page,'lxml')
print(chapter_page)

打開目錄網(wǎng)頁,發(fā)現(xiàn)在正文的目錄前面有一個最新章節(jié)目錄(這里有九個章節(jié)),再完整的目錄中是包含最新章節(jié)的,所以這里最新章節(jié)是不需要的。

在網(wǎng)頁單擊右鍵選擇“檢查”(或者“屬性”,不同的瀏覽器的叫法不一致,我用的是IE)選擇“元素”列,鼠標(biāo)再右側(cè)代碼塊上移動時。左側(cè)網(wǎng)頁會高亮顯示其對應(yīng)網(wǎng)頁區(qū)域,找到完整目錄對應(yīng)的代碼塊。如下圖:

完整目錄的錨有兩個,分別是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é)果):

對比小說章節(jié)內(nèi)容網(wǎng)址和目錄網(wǎng)址(base_url)發(fā)現(xiàn),我們只需要將base_url和章節(jié)內(nèi)容網(wǎng)址的后半段拼接到一起就可以得到完整的章節(jié)內(nèi)容網(wǎng)址

3.拼接地址

利用正則語言庫將地址后半段提取出來

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:
       定義一個列表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)

4.分析章節(jié)內(nèi)容結(jié)構(gòu)

打開章節(jié),右鍵→“屬性”,查看內(nèi)容結(jié)構(gòu),發(fā)現(xiàn)小說正文有class和id兩個錨,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)

5.保存文本

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]))

6.完整代碼

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • pycharm 多行批量縮進和反向縮進快捷鍵介紹
  • 基于pycharm實現(xiàn)批量修改變量名
  • pycharm使用正則表達式批量添加print括號完美從python2遷移到python3
  • pycharm 批量修改變量名稱的方法
  • 在PyCharm中批量查找及替換的方法

標(biāo)簽:山西 喀什 海南 安康 濟南 山西 崇左 長沙

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用PyCharm批量爬取小說的完整代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266