1编写一个程序在窗口中显示一个Hello World!的标签。2编写一个程序在窗口中显示两个文本框和一个按钮。当用户单击按钮时程序应该将两个文本框中的内容相加并在标签中显示结果。3编写一个程序在窗口中显示一个列表框和一个按钮。当用户单击按钮时程序应该将选定的项从列表框中删除。4编写一个程序爬取百度贴吧中某个贴子的所有图片。5编写一个程序爬取新浪新闻的热门新闻标题和链接。
- Python代码:
import tkinter as tk
window = tk.Tk()
label = tk.Label(window, text="Hello, World!")
label.pack()
window.mainloop()
- Python代码:
import tkinter as tk
def add_numbers():
result = int(entry1.get()) + int(entry2.get())
label.config(text="Result: " + str(result))
window = tk.Tk()
entry1 = tk.Entry(window)
entry1.pack()
entry2 = tk.Entry(window)
entry2.pack()
button = tk.Button(window, text="Add", command=add_numbers)
button.pack()
label = tk.Label(window)
label.pack()
window.mainloop()
- Python代码:
import tkinter as tk
def delete_selected_item():
selected_item = listbox.curselection()
listbox.delete(selected_item)
window = tk.Tk()
listbox = tk.Listbox(window)
listbox.pack()
listbox.insert(tk.END, "Item 1")
listbox.insert(tk.END, "Item 2")
listbox.insert(tk.END, "Item 3")
button = tk.Button(window, text="Delete", command=delete_selected_item)
button.pack()
window.mainloop()
- Python代码:
import requests
from bs4 import BeautifulSoup
import os
url = "贴子的URL"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
image_tags = soup.find_all("img")
if not os.path.exists("images"):
os.makedirs("images")
for tag in image_tags:
image_url = tag.get("src")
if image_url.startswith("http"):
response = requests.get(image_url)
file_name = image_url.split("/")[-1]
with open("images/" + file_name, "wb") as file:
file.write(response.content)
- Python代码:
import requests
from bs4 import BeautifulSoup
url = "新浪新闻的URL"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
news_list = soup.find_all("a", class_="news-link")
for news in news_list:
title = news.get("title")
link = news.get("href")
print(title, link)
请注意,在实际使用中,需要根据具体的网页结构和需求进行适当的修改。
原文地址: https://www.cveoy.top/t/topic/hFPl 著作权归作者所有。请勿转载和采集!