室友喊着沒有小說看,讓我給他推薦幾本,這能難倒我?
分分鍾就用python給他把整個網站的小說都給下載下來了,不愧是我啊!
話不多說,我們直接開整!文末附視頻
軟件
python 3.8
pycharm
使用的模塊
requests >>> pip install requests 數據請求 parsel >>> pip install parsel 數據解析 (完善功能) 添加搜索功能 搜索小說名字或者作者名字 tqdm >>> pip install tqdm 下載進度條顯示模塊 # Python學習交流群:924040232 pandas >>> pip install pandas 輸入的格式好看一些
win + R 輸入cmd 輸入安裝命令 pip install 模塊名 ;
如果出現爆紅,可能是因為,網絡連接超時,切換國內鏡像源;
黃色是警告 ,可以忽略;
輸入小說名下載
打包成exe程序, 是需要安裝 pyinstaller ,命令提示符窗口輸入 pip install pyinstaller
打包命令:pyinstaller -F -w 加上文件名(如:123.py)
下方我會放上視頻,可以對照視頻講解的更加清楚。
# 導入數據請求模塊 import requests # 導入數據解析模塊 import parsel # 導入正則表達式模塊 import re # 導入pandas import pandas as pd # 導入進度條顯示模塊 from tqdm import tqdm while True: key_word = input('請輸入你想要下載的小說名字(輸入0即可退出): ') if key_word == '0': break search_url = f'https://www.***.com/search.php?q={key_word}' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' } response = requests.get(url=search_url, headers=headers) # print(response.text) selector_1 = parsel.Selector(response.text) divs = selector_1.css('.result-list div.result-item') # print(divs) if divs: lis = [] for div in divs: novel_name = div.css('.result-game-item-title-link::attr(title)').get() # 小說名字 href = div.css('.result-game-item-title-link::attr(href)').get().split('/')[2] # ID author = div.css('.result-game-item-info p:nth-child(1) span:nth-child(2)::text').get() # 作者 # print(novel_name, href, author) dit = { '書名': novel_name, '作者': author, '書ID': href, } lis.append(dit) print(f'一共搜索到{len(lis)}條數據內容, 結果如下') search_data = pd.DataFrame(lis) print(search_data) key_num = input('請選擇你想要下載小說序號: ') # 輸入的數據類型字符串數據 novel_id = lis[int(key_num)]['書ID'] url = f'https://www.***.com/book/{novel_id}/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' } response = requests.get(url, headers) # print(response.text) # 正則表達式提取出來數據返回列表 ['天道修改器'] novel_name = re.findall('<h1>(.*?)</h1>', response.text)[0] novel_info = re.findall('<dd><a href="(.*?)" >(.*?)</a></dd>', response.text) # print(novel_name) # print(novel_info) for novel_url, novel_title in tqdm(novel_info): # 'https://www.***e.com/book/60126/14362.html' novel_url = 'https://www.***.com' + novel_url # print(novel_url, novel_title) # 1. 發送請求, 對於剛剛分析得到的url地址發送請求 # url = 'https://www.biqugee.com/book/60126/14362.html' response = requests.get(novel_url, headers) # <Response [200]> 返回response響應對象, 200表示請求成功 # 2. 獲取數據, 獲取服務器返回的response響應數據 # response.text 獲取響應體返回文本數據(網頁源代碼) # print(response.text) # 3. 解析數據, 提取我們想要的數據內容 小說章節名字 以及小說內容 # 提取數據方式: xpath css re 這三種方式都是可以提取數據 selector = parsel.Selector(response.text) # 把獲取到的response.text 轉換成 selector 對象 # novel_title = selector.css('.bookname h1::text').get() # get獲取第一個標簽數據 返回字符串數據 # novel_title_1 = selector.xpath('//*[@class="bookname"]/h1/text()').get() # get獲取第一個標簽數據 返回字符串數據 novel_content_list = selector.css('#content::text').getall() # getall 獲取所有標簽內容, 返回列表數據 # 需要把列表轉成字符串數據 join \n換行符 novel_content = '\n'.join(novel_content_list) # print(novel_title) # print(novel_title_1) # print(novel_content_list) # print(novel_content) # 4. 保存數據 # w寫入數據但是覆蓋 a寫入追加寫入, 寫入文件末尾 b 二進制模式 """ 第一章 xxx 小說內容 第二章 xxx 小說內容 """ with open(novel_name + '.txt', mode='a', encoding='utf-8') as f: f.write(novel_title) f.write('\n') f.write(novel_content) f.write('\n') # print('正在保存', novel_title) else: print('請正確輸入小說名字或者作者名字 / 沒有這本書的數據..')
視頻講解:https://www.bilibili.com/video/BV1FT4y1X7B2/
好了,今天的分享就到這里,大家快去試試吧!