基于Python3制作一個(gè)帶GUI界面的小說(shuō)爬蟲(chóng)工具
效果圖
最近幫朋友寫個(gè)簡(jiǎn)單爬蟲(chóng),順便整理了下,搞成了一個(gè)帶GUI界面的小說(shuō)爬蟲(chóng)工具,用來(lái)從筆趣閣爬取小說(shuō)。
開(kāi)發(fā)完成后的界面

采集過(guò)程界面

采集后存儲(chǔ)

主要功能
1.多線程采集,一個(gè)線程采集一本小說(shuō)
2.支持使用代理,尤其是多線程采集時(shí),不使用代理可能封ip

3.實(shí)時(shí)輸出采集結(jié)果

使用 threading.BoundedSemaphore() pool_sema.acquire() pool_sema.release() 來(lái)限制線程數(shù)量,防止并發(fā)線程過(guò)。具體限制數(shù)量,可在軟件界面輸入,默認(rèn)5個(gè)線程

# 所有線程任務(wù)開(kāi)始前 pool_sema.threading.BoundedSemaphore(5) # 具體每個(gè)線程開(kāi)始前 鎖 pool_sema.acquire() .... # 線程任務(wù)執(zhí)行結(jié)束釋放 pol_sema.release()
用到的第三方模塊
pip install requests pip install pysimplegui pip install lxml pip install pyinstaller
GUI 界面使用了一個(gè)tkinter 的封裝庫(kù) PySimpleGUI, 使用非常方便,雖然界面不夠漂亮,但勝在簡(jiǎn)單,非常適合開(kāi)發(fā)些小工具。https://pysimplegui.readthedocs.io/en/latest/比如這個(gè)界面的布局,只需簡(jiǎn)單幾個(gè) list
layout = [
[sg.Text('輸入要爬取的小說(shuō)網(wǎng)址,點(diǎn)此打開(kāi)筆趣閣站點(diǎn)復(fù)制', font=("微軟雅黑", 12),
key="openwebsite", enable_events=True, tooltip="點(diǎn)擊在瀏覽器中打開(kāi)")],
[sg.Text("小說(shuō)目錄頁(yè)url,一行一個(gè):")],
[
sg.Multiline('', key="url", size=(120, 6), autoscroll=True, expand_x=True, right_click_menu=['&Right', ['粘貼']]
)
],
[sg.Text(visible=False, text_color="#ff0000", key="error")],
[
sg.Button(button_text='開(kāi)始采集', key="start", size=(20, 1)),
sg.Button(button_text='打開(kāi)下載目錄', key="opendir",
size=(20, 1), button_color="#999999")
],
[sg.Text('填寫ip代理,有密碼格式 用戶名:密碼@ip:端口,無(wú)密碼格式 ip:端口。如 demo:123456@123.1.2.8:8580')],
[
sg.Input('', key="proxy"),
sg.Text('線程數(shù)量:'),
sg.Input('5', key="threadnum"),
],
[
sg.Multiline('等待采集', key="res", disabled=True, border_width=0, background_color="#ffffff", size=(
120, 6), no_scrollbar=False, autoscroll=True, expand_x=True, expand_y=True, font=("宋體", 10), text_color="#999999")
],
]打包為 exe 命令
pyinstaller -Fw start.py
全部源碼
import time
import requests
import os
import sys
import re
import random
from lxml import etree
import webbrowser
import PySimpleGUI as sg
import threading
# user-agent
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
}
# 代理
proxies = {}
# 刪除書(shū)名中特殊符號(hào)
# 筆趣閣基地址
baseurl = 'https://www.xbiquwx.la/'
# 線程數(shù)量
threadNum = 6
pool_sema = None
THREAD_EVENT = '-THREAD-'
cjstatus = False
# txt存儲(chǔ)目錄
filePath = os.path.abspath(os.path.join(os.getcwd(), 'txt'))
if not os.path.exists(filePath):
os.mkdir(filePath)
# 刪除特殊字符
def deletetag(text):
return re.sub(r'[\[\]#\/\\:*\,;\?\"\'<>\|\(\)《》&\^!~=%\{\}@!:?!ぃ。ぁǎ?]','',text)
# 入口
def main():
global cjstatus, proxies, threadNum, pool_sema
sg.theme("reddit")
layout = [
[sg.Text('輸入要爬取的小說(shuō)網(wǎng)址,點(diǎn)此打開(kāi)筆趣閣站點(diǎn)復(fù)制', font=("微軟雅黑", 12),
key="openwebsite", enable_events=True, tooltip="點(diǎn)擊在瀏覽器中打開(kāi)")],
[sg.Text("小說(shuō)目錄頁(yè)url,一行一個(gè):")],
[
sg.Multiline('', key="url", size=(120, 6), autoscroll=True, expand_x=True, right_click_menu=['&Right', ['粘貼']]
)
],
[sg.Text(visible=False, text_color="#ff0000", key="error")],
[
sg.Button(button_text='開(kāi)始采集', key="start", size=(20, 1)),
sg.Button(button_text='打開(kāi)下載目錄', key="opendir",
size=(20, 1), button_color="#999999")
],
[sg.Text('填寫ip代理,有密碼格式 用戶名:密碼@ip:端口,無(wú)密碼格式 ip:端口。如 demo:123456@123.1.2.8:8580')],
[
sg.Input('', key="proxy"),
sg.Text('線程數(shù)量:'),
sg.Input('5', key="threadnum"),
],
[
sg.Multiline('等待采集', key="res", disabled=True, border_width=0, background_color="#ffffff", size=(
120, 6), no_scrollbar=False, autoscroll=True, expand_x=True, expand_y=True, font=("宋體", 10), text_color="#999999")
],
]
window = sg.Window('采集筆趣閣小說(shuō)', layout, size=(800, 500), resizable=True,)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'close': # if user closes window or clicks cancel
break
if event == "openwebsite":
webbrowser.open('%s' % baseurl)
elif event == 'opendir':
os.system('start explorer ' + filePath)
elif event == 'start':
if cjstatus:
cjstatus = False
window['start'].update('已停止...點(diǎn)擊重新開(kāi)始')
continue
window['error'].update("", visible=False)
urls = values['url'].strip().split("\n")
lenth = len(urls)
for k, url in enumerate(urls):
if (not re.match(r'%s\d+_\d+/' % baseurl, url.strip())):
if len(url.strip()) > 0:
window['error'].update("地址錯(cuò)誤:%s" % url, visible=True)
del urls[k]
if len(urls) < 1:
window['error'].update(
"每行地址需符合 %s84_84370/ 形式" % baseurlr, visible=True)
continue
# 代理
if len(values['proxy']) > 8:
proxies = {
"http": "http://%s" % values['proxy'],
"https": "http://%s" % values['proxy']
}
# 線程數(shù)量
if values['threadnum'] and int(values['threadnum']) > 0:
threadNum = int(values['threadnum'])
pool_sema = threading.BoundedSemaphore(threadNum)
cjstatus = True
window['start'].update('采集中...點(diǎn)擊停止')
window['res'].update('開(kāi)始采集')
for url in urls:
threading.Thread(target=downloadbybook, args=(
url.strip(), window,), daemon=True).start()
elif event == "粘貼":
window['url'].update(sg.clipboard_get())
print("event", event)
if event == THREAD_EVENT:
strtext = values[THREAD_EVENT][1]
window['res'].update(window['res'].get()+"\n"+strtext)
cjstatus = False
window.close()
#下載
def downloadbybook(page_url, window):
try:
bookpage = requests.get(url=page_url, headers=header, proxies=proxies)
except Exception as e:
window.write_event_value(
'-THREAD-', (threading.current_thread().name, '\n請(qǐng)求 %s 錯(cuò)誤,原因:%s' % (page_url, e)))
return
if not cjstatus:
return
# 鎖線程
pool_sema.acquire()
if bookpage.status_code != 200:
window.write_event_value(
'-THREAD-', (threading.current_thread().name, '\n請(qǐng)求%s錯(cuò)誤,原因:%s' % (page_url, page.reason)))
return
bookpage.encoding = 'utf-8'
page_tree = etree.HTML(bookpage.text)
bookname = page_tree.xpath('//div[@id="info"]/h1/text()')[0]
bookfilename = filePath + '/' + deletetag(bookname)+'.txt'
zj_list = page_tree.xpath(
'//div[@class="box_con"]/div[@id="list"]/dl/dd')
for _ in zj_list:
if not cjstatus:
break
zjurl = page_url + _.xpath('./a/@href')[0]
zjname = _.xpath('./a/@title')[0]
try:
zjpage = requests.get(
zjurl, headers=header, proxies=proxies)
except Exception as e:
window.write_event_value('-THREAD-', (threading.current_thread(
).name, '\n請(qǐng)求%s:%s錯(cuò)誤,原因:%s' % (zjname, zjurl, zjpage.reason)))
continue
if zjpage.status_code != 200:
window.write_event_value('-THREAD-', (threading.current_thread(
).name, '\n請(qǐng)求%s:%s錯(cuò)誤,原因:%s' % (zjname, zjurl, zjpage.reason)))
return
zjpage.encoding = 'utf-8'
zjpage_content = etree.HTML(zjpage.text).xpath('//div[@id="content"]/text()')
content = "\n【"+zjname+"】\n"
for _ in zjpage_content:
content += _.strip() + '\n'
with open(bookfilename, 'a+', encoding='utf-8') as fs:
fs.write(content)
window.write_event_value(
'-THREAD-', (threading.current_thread().name, '\n%s:%s 采集成功' % (bookname, zjname)))
time.sleep(random.uniform(0.05, 0.2))
# 下載完畢
window.write_event_value('-THREAD-', (threading.current_thread(
).name, '\n請(qǐng)求 %s 結(jié)束' % page_url))
pool_sema.release()
if __name__ == '__main__':
main()
以上就是基于Python3制作一個(gè)帶GUI界面的小說(shuō)爬蟲(chóng)工具的詳細(xì)內(nèi)容,更多關(guān)于Python3小說(shuō)爬蟲(chóng)工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python+request+unittest實(shí)現(xiàn)接口測(cè)試框架集成實(shí)例
這篇文章主要介紹了Python+request+unittest實(shí)現(xiàn)接口測(cè)試框架集成實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
在Python中使用全局日志時(shí)需要注意的問(wèn)題
這篇文章主要介紹了在Python中使用全局日志時(shí)需要注意的問(wèn)題, 作者由uliweb使用時(shí)遇到的問(wèn)題分析全局日志出現(xiàn)錯(cuò)誤時(shí)的解決方法,需要的朋友可以參考下2015-05-05
使用python實(shí)現(xiàn)ftp的文件讀寫方法
今天小編就為大家分享一篇使用python實(shí)現(xiàn)ftp的文件讀寫方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
pycharm 在windows上編輯代碼用linux執(zhí)行配置的方法
今天小編就為大家分享一篇pycharm 在windows上編輯代碼用linux執(zhí)行配置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python網(wǎng)絡(luò)請(qǐng)求使用Requests庫(kù)抓取解析數(shù)據(jù)
在網(wǎng)絡(luò)編程中,請(qǐng)求和接收數(shù)據(jù)是最常見(jiàn)的任務(wù)之一,Python的Requests庫(kù)提供了豐富的功能,使得HTTP請(qǐng)求變得非常簡(jiǎn)單,在本文中,我們將了解如何使用Requests庫(kù)發(fā)起HTTP請(qǐng)求,并解析返回的數(shù)據(jù)2023-08-08

