爬蟲爬取小說網站的內容,並將各章節輸出到各txt文件

一、確定網站鏈接

代碼用到的鏈接,是在 https://www.biqukan.com 主頁選的一個連載小說的鏈接

from bs4 import BeautifulSoup
import requests

link = 'https://www.biqukan.com/1_1094'

二、查看網頁源代碼

發現:
1、網站是gbk編碼的
在這裏插入圖片描述
2、章節都是有a標籤的,要過濾出來這部分內容
3、我們要的是從正文捲開始的章節,想到切片截取
在這裏插入圖片描述

# 獲取結果res,編碼是gbk(這個網站就是gbk的編碼)
res = requests.get(link)
res.encoding = 'gbk'

# 使用BeatifulSoup得到網站中的文本內容
soup = BeautifulSoup(res.text)
lis = soup.find_all('a')	# 
lis = lis[42:-13]           # 不屬於章節內容的都去掉
# 用urllist存儲所有{章節名稱:鏈接}
urldict = {}

# 觀察小說各個章節的網址,結合後面的代碼,這裏只保留 split_link = 'https://www.biqukan.com/'
tmp = link.split("/")
split_link = "{0}//{1}/".format(tmp[0], tmp[2])

# 將各章節名字及鏈接形成鍵值對形式,並添加到大字典 urldict中
for i in range(len(lis)):
    print({lis[i].string: split_link + lis[i].attrs['href']})
    urldict.update({lis[i].string: split_link + lis[i].attrs['href']})

from tqdm import tqdm
for key in tqdm(urldict.keys()):
    tmplink = urldict[key]          # 章節鏈接
    res = requests.get(tmplink)     # 鏈接對應的資源文件html
    res.encoding = 'gbk'

    soup = BeautifulSoup(res.text)  # 取資源文件中的文本內容
    content = soup.find_all('div', id='content')[0]  # 取得資源文件中文本內容的小說內容

    with open('text{}.txt'.format(key), 'a+', encoding='utf8') as f:
        f.write(content.text.replace('\xa0', ''))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章