用Python手把手教你实现一个爬虫(含前端界面)_python爬虫如何实现界面操作
import tkinter as tk
import requests
from bs4 import BeautifulSoup
def fetch_quotes(symbol):
url = f"https://finance.yahoo.com/quote/{symbol}"
response = requests.get(url)
if response.ok:
soup = BeautifulSoup(response.text, 'html.parser')
name = soup.find(id='quote-header-info').find('h1').text
price = soup.find(class_='My<span class="katex">\(b\)</span>').text
return f"{name} - {price}"
else:
return "Error fetching quote"
def update_quote(symbol):
quote = fetch_quotes(symbol.get())
quote_label.config(text=quote)
root = tk.Tk()
root.title("Yahoo Finance Stock Quote Fetcher")
root.geometry("400x200")
symbol_label = tk.Label(root, text="Enter stock symbol:")
symbol_label.pack()
symbol_entry = tk.Entry(root)
symbol_entry.pack()
fetch_button = tk.Button(root, text="Fetch Quote", command=lambda: update_quote(symbol_entry))
fetch_button.pack()
quote_label = tk.Label(root, text="")
quote_label.pack()
root.mainloop()
这段代码使用了tkinter
库来创建一个简单的用户界面,允许用户输入股票代码,并在点击按钮后获取该股票的名称和价格。这个例子教会了如何将爬虫逻辑集成到图形用户界面中,并展示了如何使用Python进行简单的GUI编程。
评论已关闭