【发布时间】:2022-01-12 03:02:09
【问题描述】:
使用加密货币小部件实时抓取特定加密货币价格的 html,但遇到 AttributeError: 'NoneType' object has no attribute 'find'。
这个问题突然发生了,我真的很困惑为什么它没有早点出现。在此之前,我已经多次运行代码,完全没有问题。所以我的两个问题是......
为什么会突然发生这种情况?
和
我该如何解决这个问题?
from tkinter import *
import requests
from bs4 import BeautifulSoup
from tkinter.ttk import *
from time import strftime
def get_crypto_price(coin):
url = "https://www.google.com/search?q=" + coin + "+price" # Defining link to coin
HTML = requests.get(url) # Requesting link access
soup = BeautifulSoup(HTML.text, 'html.parser') # Parser
text = soup.find("div", attrs={'class': 'BNeawe iBp4i AP7Wnd'}).find("div",
attrs={
'class': 'BNeawe iBp4i AP7Wnd'}).text # HTML Scrubber
return coin + ': ' + text + ' | '
root = Tk()
root.title('Crypto Widget 2021')
root.geometry('1080x40')
lab = Label(root)
lab.pack()
def update():
lab['text'] = get_crypto_price("Bitcoin") + get_crypto_price("Ethereum") + get_crypto_price(
"Litecoin") + get_crypto_price("Dogecoin")
lab.after(5000, update)
update()
def time():
string = strftime('%H:%M %p')
lbl.config(text=string)
lbl.after(5000, time)
lbl = Label(root, font=('times new roman', 10, 'bold'),
background='white',
foreground='black')
lbl.pack(anchor='n')
time()
root.mainloop()
【问题讨论】:
-
如果不添加堆栈跟踪,我们真的无法提供帮助。请添加实际的堆栈跟踪,以便我们进行调试
-
该站点可能已更新 - 因此,通过 'attrs={'class': '
''} 访问的类名已更改。 -
我将如何重新发现类名?我尝试在 html 中搜索新的 div 类,但没有找到任何可行的方法。
标签: python attributeerror