Python 向函数发送url

Python 向函数发送url,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我对python很陌生,但喜欢它。 我有个问题。 我希望从tkinter中的我的按钮解析到我的函数KB的url,所以当用户单击skanderborg时,它会获取url并解析到我函数中的url,但我无法让它工作 import requests from bs4 import BeautifulSoup import tkinter from tkinter import * def kb(): page = requests.get(url) soup = BeautifulSo

我对python很陌生,但喜欢它。
我有个问题。
我希望从tkinter中的我的按钮解析到我的函数KB的url,所以当用户单击skanderborg时,它会获取url并解析到我函数中的url,但我无法让它工作

import requests
from bs4 import BeautifulSoup
import tkinter
from tkinter import *

def kb():
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    seven_day = soup.find(class_="today_nowcard-container")
    forecast_items = seven_day.find_all(class_="today_nowcard-main")
    details_items = seven_day.find(class_="today_nowcard-section")
    place = seven_day.find(class_="today_nowcard-location").get_text()
    daily_temp = seven_day.find(class_="today_nowcard-temp").get_text()
    cloud = seven_day.find(class_="today_nowcard-phrase").get_text()
    print(place)
    print(daily_temp)
    print(cloud)
    data = ("place" + "cloud")
    return place, cloud, daily_temp

var2 = kb()
top = tkinter.Tk()
placevar1 = StringVar()
placevar1.set(var2)
label = Label(top, textvariable=placevar1 )
B = tkinter.Button(top, text ="Copenhagen", command = kb)
B2 = tkinter.Button(top, text ="Skanderborg", command=lambda: kb(url ="https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7"))
label.pack()
B.pack()
B2.pack()
top.mainloop()

在我看来,kb函数不需要任何参数

你在哪里

def kb():
你可能需要把

def kb(url) :

相反。这将使您的kb函数采用您调用它的URL,并在整个函数中使用它作为变量名URL

有两三件事没有进展

不要忘记@tomh1012指出的函数中的参数。 如果要更新文本,则
kb
必须接受两个参数。
变成:

小心缩进,它们少了一两个(我不知道这是不是一个坏的复制/粘贴)

如果要将命令用作lambda表达式,请不要忘记关键字
lambda:
。(您在第一个
B
按钮中忘记了它)

还有一条建议:不要犹豫使用python IDE,它简化了大量调试和语法问题(例如:)

完整的工作示例

import requests
from bs4 import BeautifulSoup
import tkinter
from tkinter import *

def kb(url, text_widget):
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    seven_day = soup.find(class_="today_nowcard-container")
    forecast_items = seven_day.find_all(class_="today_nowcard-main")
    details_items = seven_day.find(class_="today_nowcard-section")
    place = seven_day.find(class_="today_nowcard-location").get_text()
    daily_temp = seven_day.find(class_="today_nowcard-temp").get_text()
    cloud = seven_day.find(class_="today_nowcard-phrase").get_text()
    print(place)
    print(daily_temp)
    print(cloud)
    data = ("place" + "cloud")
    text_widget.set((place, cloud, daily_temp))
    return place, cloud, daily_temp


top = tkinter.Tk()
placevar1 = StringVar()
var2 = kb(url =
"https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7", text_widget=placevar1)
label = Label(top, textvariable=placevar1 )
B = tkinter.Button(top, text ="Copenhagen", command = lambda: kb(url="https://weather.com/weather/today/l/DAXX0009:1:DA", text_widget=placevar1))
B2 = tkinter.Button(top, text ="Skanderborg", command= lambda: kb(url =
"https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7", text_widget=placevar1))

label.pack()
B.pack()
B2.pack()
top.mainloop()

您的代码格式不正确:您的返回不在函数范围内;函数内部有未定义的变量
url
,Python猜不出您想要传递url,因此需要将函数的定义更改为
def kb(url):
import requests
from bs4 import BeautifulSoup
import tkinter
from tkinter import *

def kb(url, text_widget):
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    seven_day = soup.find(class_="today_nowcard-container")
    forecast_items = seven_day.find_all(class_="today_nowcard-main")
    details_items = seven_day.find(class_="today_nowcard-section")
    place = seven_day.find(class_="today_nowcard-location").get_text()
    daily_temp = seven_day.find(class_="today_nowcard-temp").get_text()
    cloud = seven_day.find(class_="today_nowcard-phrase").get_text()
    print(place)
    print(daily_temp)
    print(cloud)
    data = ("place" + "cloud")
    text_widget.set((place, cloud, daily_temp))
    return place, cloud, daily_temp


top = tkinter.Tk()
placevar1 = StringVar()
var2 = kb(url =
"https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7", text_widget=placevar1)
label = Label(top, textvariable=placevar1 )
B = tkinter.Button(top, text ="Copenhagen", command = lambda: kb(url="https://weather.com/weather/today/l/DAXX0009:1:DA", text_widget=placevar1))
B2 = tkinter.Button(top, text ="Skanderborg", command= lambda: kb(url =
"https://weather.com/weather/today/l/ad60b9f8e5bf7d9baa35aefb5e724782692ad6b7a35f43ee43caa4182838eef7", text_widget=placevar1))

label.pack()
B.pack()
B2.pack()
top.mainloop()