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

编程入门 行业动态 更新时间:2024-10-10 03:30:33

【 Python足彩网站<a href=https://www.elefans.com/category/jswz/34/1763968.html style=赔率数据文件自动下载(Tkinter+BeautifulSoup+Selenium隐藏浏览器界面,双线程)】"/>

【 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, runningtry:duration = int(time_entry.get())if duration <= 0:raise ValueErrorstart_button.config(state="disabled")time_entry.config(state="disabled")root.after(duration * 1000, enable_button)# 设置标志为True,表示线程可以继续执行running = Truedef download():while running:#传递回调函数start_selenium(update_progress,download_excel_callback)def run_in_thread():global threadthread = 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 = Falseroot.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 timedef main(m,callback,download_callback):callback('程序开始...')n=0while True:n=n+1callback('开始第%s次尝试...'%n)url = '/'callback(url)soup=Nonetry: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('*************************************************')continuecallback('二级网页获取成功。')# 获取当前时间current_time = datetime.datetime.now()# 将时间格式化为"20231222"的格式formatted_time = current_time.strftime("%Y%m%d%H%M%S")download_path = '.\\download\\' + formatted_timedownload_path=os.path.abspath(download_path)if not os.path.exists(download_path):# 如果文件夹不存在,则创建文件夹os.makedirs(download_path)i=0for d in detail_urls:i+=1callback('正在下载第%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.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from seleniummon.exceptions import TimeoutException
from seleniummon.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_pathprefs = {'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=Nonetry:# 启动浏览器并打开目标网页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=Nonewhile True:try:btn = wait.until(EC.visibility_of_element_located((By.XPATH, btn_xpath)))except TimeoutException:callback('等待超时,未找到按钮。3秒后再次尝试...')time.sleep(3)continuecallback('获取按钮。')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

*本文章仅供学习和交流

更多推荐

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

本文发布于:2024-02-14 15:25:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1764132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:足彩   赔率   界面   浏览器   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!