Python爬蟲足彩網(wǎng)站賠率數(shù)據(jù)Excel文件自動(dòng)定時(shí)下載(Tkinter+BeautifulSoup+Selenium隱藏瀏覽器界面,雙線程)
朋友為了分析足彩的實(shí)時(shí)賠率,需要每隔一段時(shí)間自動(dòng)下載網(wǎng)站上的excel數(shù)據(jù)。因此開發(fā)了這款軟件。
總共就3個(gè)代碼塊,以下是完整源代碼。
1.第一步:創(chuàng)建應(yīng)用程序界面
import tkinter as tk
from tkinter import messagebox
import tkinter.scrolledtext as scrolledtext
import auto_download
import threading
# 創(chuàng)建一個(gè)全局變量來存儲(chǔ)線程對(duì)象
thread = None
running = False # 用于控制線程是否繼續(xù)執(zhí)行的標(biāo)志
def start_selenium(callback,download_callback):
auto_download.main(time_entry.get(), callback,download_callback)
update_progress('程序開始運(yùn)行。')
def start_program():
global thread, running
try:
duration = int(time_entry.get())
if duration <= 0:
raise ValueError
start_button.config(state="disabled")
time_entry.config(state="disabled")
root.after(duration * 1000, enable_button)
# 設(shè)置標(biāo)志為True,表示線程可以繼續(xù)執(zhí)行
running = True
def download():
while running:
#傳遞回調(diào)函數(shù)
start_selenium(update_progress,download_excel_callback)
def run_in_thread():
global thread
thread = threading.Thread(target=download)
thread.daemon = True # 設(shè)置線程為守護(hù)線程,主線程退出后會(huì)自動(dòng)退出子線程
thread.start()
run_in_thread()
except ValueError:
messagebox.showerror("錯(cuò)誤", "請(qǐng)輸入有效的定時(shí)時(shí)間(大于0的整數(shù))!")
time_entry.delete(0, tk.END)
time_entry.focus()
def enable_button():
start_button.config(state="normal")
time_entry.config(state="normal")
def update_progress(info):
text_widget.insert(tk.END, info + "\n")
text_widget.see(tk.END) # 滾動(dòng)至最底部
text_widget.update() # 更新文本框
def download_excel_callback(info): # 定義下載回調(diào)函數(shù)
text_widget.insert(tk.END, info + "\n")
text_widget.see(tk.END) # 滾動(dòng)至最底部
text_widget.update() # 更新文本框
def on_closing():
global running
# 在窗體關(guān)閉時(shí)修改標(biāo)志為False,停止線程的執(zhí)行
running = False
root.quit() # 使用root.quit()代替root.destroy()
root = tk.Tk()
root.title("Selenium程序執(zhí)行界面")
root.geometry("400x250")
# 定時(shí)時(shí)間標(biāo)簽和文本框
time_label = tk.Label(root, text="執(zhí)行頻率(min):")
time_label.grid(row=0, column=0, sticky="E", pady=10)
time_entry = tk.Entry(root, justify='right')
time_entry.insert(tk.END, '60')
time_entry.grid(row=0, column=1, padx=0, pady=10)
# 多行文本框
text_widget = scrolledtext.ScrolledText(root, height=10,width=51)
text_widget.grid(row=1, columnspan=2, padx=15, pady=5)
# 開始按鈕
start_button = tk.Button(root, text="開始", command=start_program)
start_button.grid(row=2, columnspan=2, pady=10)
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
2第二步:獲所有需要下載的URL并添加到列表。循環(huán)列表。
import requests
from bs4 import BeautifulSoup
import download_excel
import os
import datetime
import time
def main(m,callback,download_callback):
callback('程序開始...')
n=0
while True:
n=n+1
callback('開始第%s次嘗試...'%n)
url = 'https://trade.500.com/jczq/'
callback(url)
soup=None
try:
response = requests.get(url)
response.raise_for_status() # 檢查請(qǐng)求是否成功,如果不是則拋出異常
soup = BeautifulSoup(response.content, 'html.parser')
# 進(jìn)一步處理頁面內(nèi)容
except requests.exceptions.RequestException as e:
# 請(qǐng)求異常處理
callback("請(qǐng)求發(fā)生異常:", e)
except Exception as e:
# 其他異常處理
callback("發(fā)生異常:", e)
table=soup.find_all('table')[2]
print(table)
callback('一級(jí)網(wǎng)頁爬取成功。')
detail_urls=[]
links = table.find_all("a")
for link in links:
if "歐" in link.text and "shtml" in link["href"]:
detail_urls.append(link["href"])
callback('發(fā)現(xiàn)目標(biāo)文件%s個(gè)'%len(detail_urls))
print(detail_urls)
if len(detail_urls)<1:
callback('二級(jí)網(wǎng)頁爬取失敗。')
time.sleep(int(m)*60)
callback('待機(jī)中,%s分鐘后繼續(xù)掃描。' % m)
callback('*************************************************')
continue
callback('二級(jí)網(wǎng)頁獲取成功。')
# 獲取當(dāng)前時(shí)間
current_time = datetime.datetime.now()
# 將時(shí)間格式化為"20231222"的格式
formatted_time = current_time.strftime("%Y%m%d%H%M%S")
download_path = '.\\download\\' + formatted_time
download_path=os.path.abspath(download_path)
if not os.path.exists(download_path):
# 如果文件夾不存在,則創(chuàng)建文件夾
os.makedirs(download_path)
i=0
for d in detail_urls:
i+=1
callback('正在下載第%s個(gè)...'%i)
callback(d)
download_excel.download(d, download_path,download_callback)
callback('第%s個(gè)下載完成。'%i)
callback('待機(jī)中,%s分鐘后繼續(xù)掃描。'%m)
callback('*************************************************')
time.sleep(int(m)*60)
3第三步:下載Excel文件。因?yàn)槭菬o頭瀏覽器,所以不能直接用Selenium點(diǎn)擊,要用Javascript。文章來源:http://www.zghlxwxcb.cn/news/detail-800273.html
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import WebDriverException
import sys
import time
def download(url,download_path,callback):
# 指定Chrome瀏覽器位置和驅(qū)動(dòng)位置
chrome_binary_path = '.\\chrome\\Application\\chrome.exe'
chrome_driver_path = '.\\chrome\\Application\\chromedriver.exe'
# 創(chuàng)建瀏覽器選項(xiàng)實(shí)例,并啟用下載文件功能
chrome_options = Options()
# 切換為無頭模式
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('–-disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument("--test-type")
chrome_options.binary_location = chrome_binary_path
prefs = {'download.default_directory': download_path } # 設(shè)置默認(rèn)下載文件路徑
chrome_options.add_experimental_option('prefs', prefs)
# 在瀏覽器選項(xiàng)中添加下面兩行代碼,指定下載路徑和禁用PDF和Flash插件
chrome_options.add_argument('--disable-plugins')
chrome_options.add_argument('--disable-extensions')
driver=None
try:
# 啟動(dòng)瀏覽器并打開目標(biāo)網(wǎng)頁
driver = webdriver.Chrome(chrome_driver_path, options=chrome_options)
driver.get(url)
except WebDriverException as e:
# 處理WebDriverException異常
callback("啟動(dòng)瀏覽器或打開目標(biāo)網(wǎng)頁時(shí)發(fā)生異常:")
callback('瀏覽器啟動(dòng)并成功獲取頁面。')
btn_xpath = "http://a[contains(text(), '賠率下載')]"
wait_time = 10 # 最大等待時(shí)間,單位為秒
wait = WebDriverWait(driver, wait_time)
btn=None
while True:
try:
btn = wait.until(EC.visibility_of_element_located((By.XPATH, btn_xpath)))
except TimeoutException:
callback('等待超時(shí),未找到按鈕。3秒后再次嘗試...')
time.sleep(3)
continue
callback('獲取按鈕。')
driver.execute_script("arguments[0].click();", btn)
callback('點(diǎn)擊按鈕。')
callback('下載中,等待3秒。')
time.sleep(3)
driver.quit()
callback('瀏覽器已關(guān)閉。')
break
4.第四部:打包應(yīng)用程序文章來源地址http://www.zghlxwxcb.cn/news/detail-800273.html
pyinstaller --hidden-import=selenium --hidden-import=tkinter --hidden-import=auto_download --hidden-import=requests --hidden-import=bs4 form.py
*本文章僅供學(xué)習(xí)和交流
到了這里,關(guān)于【 Python足彩網(wǎng)站賠率數(shù)據(jù)文件自動(dòng)下載(Tkinter+BeautifulSoup+Selenium隱藏瀏覽器界面,雙線程)】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!