Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

python - tkinter after() problem, wont pass correct value to the label

When I run the following Code, I get this:

Screenshot of Output

I want just the Bitcoin Value and also the current one, please help I am totally stuck.

from bs4 import BeautifulSoup
import requests
import tkinter as tk
from tkinter import *
import time
time = 1000



def bitcoinTracker():  
    url = "https://coinmarketcap.com/currencies/bitcoin/markets/"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'lxml')
    price = soup.find('div', {'class':'priceValue___11gHJ'}).text
    return (price)


def collector():
    label = tk.Label(text="Bitcoin " + bitcoinTracker(), font="Arial 18")
    label.pack()
    root.after(time, collector)
root.after(time, collector)
root = tk.Tk()
window.mainloop()
question from:https://stackoverflow.com/questions/65644859/tkinter-after-problem-wont-pass-correct-value-to-the-label

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you go to that website in your browser and refresh the page, you'll see that the price doesn't update right away. It seems to show a cached value right away, then updates it with recent data a few seconds later. At the time of writing, I always see $40,406.60 when initially viewing the page, then it updates after.

Without executing the Javascript that the page uses to fetch the updated info, you'll only see the old, cached data, until they update the cached data (it seems to happen every few minutes?).

You'd need to use a library like Selenium that is capable of executing Javascript. requests just gives you a single snapshot of the page at the time of fetching, and doesn't allow for anything else, like execution of Javascript to happen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...