日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

爬虫爬取在线小说阅读网站详解

發布時間:2025/3/19 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫爬取在线小说阅读网站详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言


環境:python 安裝、requests安裝、Beautiful Soup安裝

爬取目標:

筆趣看網站的《校花之貼身高手》,以下是第一章鏈接

https://www.biqukan.com/11_11499/4260511.html

開始爬取


1.打開鏈接,打開檢查窗口

通過審查Elements,能定位到小說的文本內容在<div id="content" class="showtxt">里面


2.借用requests庫將頁面的HTML內容先爬取下來

新建文件getnovel1.py

import requestsif __name__ == '__main__':print((requests.get(url='https://www.biqukan.com/11_11499/4260511.html')).text)


然后運行代碼:

解析HTML信息


爬取網頁的HTML信息后,就要使用Beautiful Soup 來解析HTML信息

1.安裝Beautiful Soup

pip install beautifulsoup4

2.在解析html之前,我們需要創建一個Beautiful Soup 對象。

Beautiful Soup函數的參數就是之前獲取到的html信息。

然后使用find_all()方法,獲得html信息中所有class屬性為showtxt的div標簽。

find_all方法的第一個參數是獲取的標簽名,第二個參數是標簽的屬性,帶了一個下劃線是因為class是python中的關鍵字。

新建文件 getnovel2.py

import requests from bs4 import BeautifulSoupif __name__=="__main__":html = requests.get(url='https://www.biqukan.com/11_11499/4260511.html').textnovel = BeautifulSoup(html).find_all('div',class_='showtxt')print(novel)


???
運行:

3.但是爬取的內容還是有一些不如人意,我們要想法去除掉div 標簽、br標簽等。

新建文件getnovel3.py

import requests from bs4 import BeautifulSoupif __name__=="__main__":html = requests.get(url='https://www.biqukan.com/11_11499/4260511.html').textnovel = BeautifulSoup(html).find_all('div',class_='showtxt')print(novel[0].text.replace('\xa0'*8,'\n\n'))

分析:

find_all匹配的返回結果是列表,使用tnovel[0]獲取匹配結果,使用text屬性,提取文本內容,
這樣能過濾掉br標簽。

然后使用replace方法,替換空格為回車,為什么是8格空格,再看網頁結構。

運行結果:

爬取目錄信息

1.上面已經爬取了第一章的內容,想要下載所有章節,先來分析下小說目錄。

2.從上面我們知道,這些章節存放在了class屬性為listmain的div標簽下的dl下的dd下的a標簽中,

而且這些a標簽的href屬性前面都是一樣的,只有后面部分不一樣。

3.對于BeautifulSoup返回的匹配結果a,使用a.get('href')方法就能獲取href的屬性值,使用a.string就能獲取章節的名字。

4.新建文件getnovel4.py

from bs4 import BeautifulSoup import requests if __name__ == "__main__":server = 'https://www.biqukan.com/'target = 'https://www.biqukan.com/11_11499/'req = requests.get(url = target)html = req.textdiv_bf = BeautifulSoup(html)div = div_bf.find_all('div', class_ = 'listmain')a_bf = BeautifulSoup(str(div[0]))a = a_bf.find_all('a')for each in a:print(each.string, server + each.get('href'))

備注:

小說每張章的鏈接放在了class屬性為listmain的div標簽下的<a>中。先匹配class屬性為listmain的<div>標簽,再匹配

<a>標簽。接下來再匹配每一個<a>標簽,并提取章節名和章節文章。因為find_all返回的是一個列表,里邊存放了很多的<a>標簽,

所以使用for 循環遍歷每個<a>標簽并打印出來。

運行:

5.整合代碼

# -*- coding:UTF-8 -*- from bs4 import BeautifulSoup import requests, sysclass downloader(object):def __init__(self):self.server = 'http://www.biqukan.com/'self.target = 'https://www.biqukan.com/11_11499/'self.names = []??????????? #存放章節名self.urls = []??????????? #存放章節鏈接self.nums = 0??????????? #章節數def get_download_url(self):req = requests.get(url = self.target)html = req.textdiv_bf = BeautifulSoup(html)div = div_bf.find_all('div', class_ = 'listmain')a_bf = BeautifulSoup(str(div[0]))a = a_bf.find_all('a')self.nums = len(a[15:])??????????????????????????????? #剔除不必要的章節,并統計章節數for each in a[15:]:self.names.append(each.string)self.urls.append(self.server + each.get('href'))def get_contents(self, target):req = requests.get(url = target)html = req.textbf = BeautifulSoup(html)texts = bf.find_all('div', class_ = 'showtxt')texts = texts[0].text.replace('\xa0'*8,'\n\n')return textsdef writer(self, name, path, text):write_flag = Truewith open(path, 'a', encoding='utf-8') as f:f.write(name + '\n')f.writelines(text)f.write('\n\n')if __name__ == "__main__":dl = downloader()dl.get_download_url()print('《小說》開始下載:')for i in range(dl.nums):dl.writer(dl.names[i], '小說.txt', dl.get_contents(dl.urls[i]))sys.stdout.write("? 已下載:%.3f%%" %? float(i/dl.nums) + '\r')sys.stdout.flush()print('《小說》下載完成')

?

運行

總結

以上是生活随笔為你收集整理的爬虫爬取在线小说阅读网站详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。