bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

利用Python爬蟲怎么對筆趣閣小說進行爬取-創新互聯

今天就跟大家聊聊有關利用Python爬蟲怎么對筆趣閣小說進行爬取,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

創新互聯公司長期為上千余家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為洛南企業提供專業的網站設計、成都網站建設,洛南網站改版等技術服務。擁有10多年豐富建站經驗和眾多成功案例,為您定制開發。

基本開發環境

  • Python 3.6

  • Pycharm

相關模塊的使用

  • request

  • sparsel

安裝Python并添加到環境變量,pip安裝需要的相關模塊即可。

利用Python爬蟲怎么對筆趣閣小說進行爬取

單章爬取

利用Python爬蟲怎么對筆趣閣小說進行爬取

一、明確需求

爬取小說內容保存到本地

  • 小說名字

  • 小說章節名字

  • 小說內容

# 第一章小說url地址
url = 'http://www.biquges.com/52_52642/25585323.html'
url = 'http://www.biquges.com/52_52642/25585323.html'
headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)

利用Python爬蟲怎么對筆趣閣小說進行爬取

請求網頁返回的數據中出現了亂碼,這就需要我們轉碼了。

加一行代碼自動轉碼。

response.encoding = response.apparent_encoding

利用Python爬蟲怎么對筆趣閣小說進行爬取

三、解析數據

利用Python爬蟲怎么對筆趣閣小說進行爬取

根據css選擇器可以直接提取小說標題以及小說內容。

def get_one_novel(html_url):
 # 調用請求網頁數據函數
 response = get_response(html_url)
 # 轉行成selector解析對象
 selector = parsel.Selector(response.text)
 # 獲取小說標題
 title = selector.css('.bookname h2::text').get()
 # 獲取小說內容 返回的是list
 content_list = selector.css('#content::text').getall()
 # ''.join(列表) 把列表轉換成字符串
 content_str = ''.join(content_list)
 print(title, content_str)

if __name__ == '__main__':
 url = 'http://www.biquges.com/52_52642/25585323.html'
 get_one_novel(url)

利用Python爬蟲怎么對筆趣閣小說進行爬取

四、保存數據(數據持久化)

使用常用的保存方式:with open

def save(title, content):
 """
 保存小說
 :param title: 小說章節標題
 :param content: 小說內容
 :return: 
 """
 # 路徑
 filename = f'{title}\\'
 # os 內置模塊,自動創建文件夾
 if os.makedirs(filename):
 os.mkdir()
 # 一定要記得加后綴 .txt mode 保存方式 a 是追加保存 encoding 保存編碼
 with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:
 # 寫入標題
 f.write(title)
 # 換行
 f.write('\n')
 # 寫入小說內容
 f.write(content)

利用Python爬蟲怎么對筆趣閣小說進行爬取
利用Python爬蟲怎么對筆趣閣小說進行爬取

保存一章小說,就這樣寫完了,如果想要保存整本小說呢?

整本小說爬蟲

既然爬取單章小說知道怎么爬取了,那么只需要獲取小說所有單章小說的url地址,就可以爬取全部小說內容了。

利用Python爬蟲怎么對筆趣閣小說進行爬取

所有的單章的url地址都在 dd 標簽當中,但是這個url地址是不完整的,所以爬取下來的時候,要拼接url地址。

def get_all_url(html_url):
 # 調用請求網頁數據函數
 response = get_response(html_url)
 # 轉行成selector解析對象
 selector = parsel.Selector(response.text)
 # 所有的url地址都在 a 標簽里面的 href 屬性中 
 dds = selector.css('#list dd a::attr(href)').getall()
 for dd in dds:
 novel_url = 'http://www.biquges.com' + dd
 print(novel_url)


if __name__ == '__main__':
 url = 'http://www.biquges.com/52_52642/index.html'
 get_all_url(url)

利用Python爬蟲怎么對筆趣閣小說進行爬取

這樣就獲取了所有的小說章節url地址了。

爬取全本完整代碼

import requests
import parsel
from tqdm import tqdm


def get_response(html_url):
 headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
 }
 response = requests.get(url=html_url, headers=headers)
 response.encoding = response.apparent_encoding
 return response


def save(novel_name, title, content):
 """
 保存小說
 :param title: 小說章節標題
 :param content: 小說內容
 :return:
 """
 filename = f'{novel_name}' + '.txt'
 # 一定要記得加后綴 .txt mode 保存方式 a 是追加保存 encoding 保存編碼
 with open(filename, mode='a', encoding='utf-8') as f:
 # 寫入標題
 f.write(title)
 # 換行
 f.write('\n')
 # 寫入小說內容
 f.write(content)


def get_one_novel(name, novel_url):
 # 調用請求網頁數據函數
 response = get_response(novel_url)
 # 轉行成selector解析對象
 selector = parsel.Selector(response.text)
 # 獲取小說標題
 title = selector.css('.bookname h2::text').get()
 # 獲取小說內容 返回的是list
 content_list = selector.css('#content::text').getall()
 # ''.join(列表) 把列表轉換成字符串
 content_str = ''.join(content_list)
 save(name, title, content_str)


def get_all_url(html_url):
 # 調用請求網頁數據函數
 response = get_response(html_url)
 # 轉行成selector解析對象
 selector = parsel.Selector(response.text)
 # 所有的url地址都在 a 標簽里面的 href 屬性中
 dds = selector.css('#list dd a::attr(href)').getall()
 # 小說名字
 novel_name = selector.css('#info h2::text').get()
 for dd in tqdm(dds):
 novel_url = 'http://www.biquges.com' + dd
 get_one_novel(novel_name, novel_url)

if __name__ == '__main__':
 novel_id = input('輸入書名ID:')
 url = f'http://www.biquges.com/{novel_id}/index.html'
 get_all_url(url)

利用Python爬蟲怎么對筆趣閣小說進行爬取



看完上述內容,你們對利用Python爬蟲怎么對筆趣閣小說進行爬取有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯行業資訊頻道,感謝大家的支持。

本文名稱:利用Python爬蟲怎么對筆趣閣小說進行爬取-創新互聯
文章路徑:http://vcdvsql.cn/article18/ceocdp.html

成都網站建設公司_創新互聯,為您提供網站收錄App設計營銷型網站建設響應式網站移動網站建設網站維護

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司