【 Python足彩网站赔率数据文件自动下载(Tkinter+BeautifulSoup+Selenium隐藏浏览器界面,双线程)】

Python爬虫足彩网站赔率数据Excel文件自动定时下载(Tkinter+BeautifulSoup+Selenium隐藏浏览器界面,双线程)

朋友为了分析足彩的实时赔率,需要每隔一段时间自动下载网站上的excel数据。因此开发了这款软件。在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总共就3个代码块,以下是完整源代码。

1.第一步:创建应用程序界面

import tkinter as tk
from tkinter import messagebox
import tkinter.scrolledtext as scrolledtext
import auto_download
import threading

# 创建一个全局变量来存储线程对象
thread = None
running = False  # 用于控制线程是否继续执行的标志


def start_selenium(callback,download_callback):
    auto_download.main(time_entry.get(), callback,download_callback)
    update_progress('程序开始运行。')

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)

        # 设置标志为True,表示线程可以继续执行
        running = True
        def download():
            while running:
                #传递回调函数
                start_selenium(update_progress,download_excel_callback)

        def run_in_thread():
            global thread
            thread = threading.Thread(target=download)
            thread.daemon = True  # 设置线程为守护线程,主线程退出后会自动退出子线程
            thread.start()

        run_in_thread()
    except ValueError:
        messagebox.showerror("错误", "请输入有效的定时时间(大于0的整数)!")
        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)  # 滚动至最底部
    text_widget.update()  # 更新文本框
def download_excel_callback(info):  # 定义下载回调函数
    text_widget.insert(tk.END, info + "\n")
    text_widget.see(tk.END)  # 滚动至最底部
    text_widget.update()  # 更新文本框

def on_closing():
    global running
    # 在窗体关闭时修改标志为False,停止线程的执行
    running = False
    root.quit()  # 使用root.quit()代替root.destroy()

root = tk.Tk()
root.title("Selenium程序执行界面")
root.geometry("400x250")

# 定时时间标签和文本框
time_label = tk.Label(root, text="执行频率(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并添加到列表。循环列表。

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()  # 检查请求是否成功,如果不是则抛出异常
            soup = BeautifulSoup(response.content, 'html.parser')

            # 进一步处理页面内容

        except requests.exceptions.RequestException as e:
            # 请求异常处理
            callback("请求发生异常:", e)

        except Exception as e:
            # 其他异常处理
            callback("发生异常:", e)
        table=soup.find_all('table')[2]
        print(table)
        callback('一级网页爬取成功。')
        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('发现目标文件%s个'%len(detail_urls))
        print(detail_urls)
        if len(detail_urls)<1:
            callback('二级网页爬取失败。')
            time.sleep(int(m)*60)
            callback('待机中,%s分钟后继续扫描。' % m)
            callback('*************************************************')
            continue
        callback('二级网页获取成功。')
        # 获取当前时间
        current_time = datetime.datetime.now()

        # 将时间格式化为"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):
            # 如果文件夹不存在,则创建文件夹
            os.makedirs(download_path)
        i=0
        for d in detail_urls:
            i+=1
            callback('正在下载第%s个...'%i)
            callback(d)
            download_excel.download(d, download_path,download_callback)
            callback('第%s个下载完成。'%i)
        callback('待机中,%s分钟后继续扫描。'%m)
        callback('*************************************************')
        time.sleep(int(m)*60)


3第三步:下载Excel文件。因为是无头浏览器,所以不能直接用Selenium点击,要用Javascript。

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浏览器位置和驱动位置
    chrome_binary_path = '.\\chrome\\Application\\chrome.exe'
    chrome_driver_path = '.\\chrome\\Application\\chromedriver.exe'

    # 创建浏览器选项实例,并启用下载文件功能
    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 }  # 设置默认下载文件路径
    chrome_options.add_experimental_option('prefs', prefs)
    # 在浏览器选项中添加下面两行代码,指定下载路径和禁用PDF和Flash插件
    chrome_options.add_argument('--disable-plugins')
    chrome_options.add_argument('--disable-extensions')
    driver=None
    try:
        # 启动浏览器并打开目标网页
        driver = webdriver.Chrome(chrome_driver_path, options=chrome_options)
        driver.get(url)
    except WebDriverException as e:
        # 处理WebDriverException异常
        callback("启动浏览器或打开目标网页时发生异常:")
    callback('浏览器启动并成功获取页面。')
    btn_xpath = "//a[contains(text(), '赔率下载')]"
    wait_time = 10  # 最大等待时间,单位为秒

    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('等待超时,未找到按钮。3秒后再次尝试...')
            time.sleep(3)
            continue

        callback('获取按钮。')
        driver.execute_script("arguments[0].click();", btn)
        callback('点击按钮。')
        callback('下载中,等待3秒。')
        time.sleep(3)
        driver.quit()
        callback('浏览器已关闭。')
        break


4.第四部:打包应用程序

pyinstaller --hidden-import=selenium --hidden-import=tkinter --hidden-import=auto_download --hidden-import=requests --hidden-import=bs4 form.py

*本文章仅供学习和交流

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值