0

I'm making a cryptocurrency watch app. I using for this requests but i always taking same requests.

coin_list = ["bitcoin", "ethereum", "bittorrent", "xrp", "dogecoin"]

while(True):
    for coin in coin_list:
        print(BeautifulSoup.get_text(BeautifulSoup(requests.get('https://www.coindesk.com/price/'+coin).content, 'html.parser').find('div', {'class': 'price-large'}))[1:])

this script returning it to me:

36,331.66
2,219.07
0.002826
0.716486
0.266623
36,331.66
2,219.07
0.002826
0.716486
0.266623
36,331.66
2,219.07
0.002826
0.716486
0.266623
36,331.66
2,219.07
0.002826
0.716486
0.266623
36,331.66
2,219.07
0.002826
0.716486
0.266623
36,331.66
2,219.07
0.002826

this is not about cache. i was try it with headers.

1 Answers1

0

Well, you're doing:

while(True):
    ...

which means that you are forever going to loop over coin_list since this condition is always True.

Here's an example in action of what is happening. (I have refactored your code for readability):

while True:
    for coin in coin_list:
        soup = BeautifulSoup(
            requests.get("https://www.coindesk.com/price/" + coin).content, "html.parser"
        )
        print(soup.find("div", {"class": "price-large"}).text)

    # The following will run after entirely looping over `coin_list`, we are still in the `while` loop
    print("Finished looping over `coin_list`")
    print()
    

Output:

$36,294.89
$2,213.96
$0.002825
$0.721100
$0.267331
Finished looping over `coin_list`

$36,294.89
$2,213.96
$0.002825
$0.721100
$0.267331
Finished looping over `coin_list`
...
... And on forever

So, to fix this, just remove the while condition, and loop over coin_list:

import requests
from bs4 import BeautifulSoup


coin_list = ["bitcoin", "ethereum", "bittorrent", "xrp", "dogecoin"]

for coin in coin_list:
    soup = BeautifulSoup(
        requests.get("https://www.coindesk.com/price/" + coin).content,
        "html.parser",
    )
    print(soup.find("div", {"class": "price-large"}).text)

Output:

$36,294.89
$2,216.34
$0.002825
$0.721100
$0.267331
MendelG
  • 8,523
  • 3
  • 16
  • 34
  • problem is not while loop problem is requests.get() always returning same response with same value how i can taking real time value in while loop – THE Bildeiz Jun 29 '21 at 20:13
  • 1
    I'm trying out the code that MendelG has posted and I get different numbers. Perhaps we ran this code at different times and got different numbers. – quamrana Jun 29 '21 at 20:17
  • @THEBildeiz What's your current problem? Please edit your question to provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – MendelG Jun 29 '21 at 20:30
  • I'm currently running this code, but with `while True:` around the `for` loop and `time.sleep(120)` after the `for`. I sometimes get different numbers. – quamrana Jun 29 '21 at 20:31
  • my problem is; my script is runing with a while loop but requests in while loop always getting same value for bitcoin and other coins it cause printing always same value – THE Bildeiz Jun 29 '21 at 20:34
  • @quamrana I couldn't reproduce, can you share a [pastebin](https://pastebin.com/). – MendelG Jun 29 '21 at 20:38
  • @THEBildeiz I honestly don't understand what you mean, maybe quamrana can post an answer to help you. – MendelG Jun 29 '21 at 20:39
  • @quamrana use MendelG script and delete other than bitcoin from list. try again you will see gettin always same value but value changing in the website – THE Bildeiz Jun 29 '21 at 20:46
  • I'm currently running the code as I said, and although the numbers don't change often, I've seen bitcoin at: `$36,389.07` and then `$36,361.40` two minutes later. (And now its changed again to: `$36,263.53`) – quamrana Jun 29 '21 at 20:49
  • thats right but why in the web site it changing per seconds – THE Bildeiz Jun 29 '21 at 20:50
  • 1
    Ok, well that's a different question. – quamrana Jun 29 '21 at 20:51
  • @THEBildeiz The data is being updated dynamically, see [Web-scraping JavaScript page with Python](https://stackoverflow.com/questions/8049520/web-scraping-javascript-page-with-python) – MendelG Jun 29 '21 at 20:56
  • @MendelG thank you but this is influence the impact of performance – THE Bildeiz Jun 29 '21 at 20:59
  • @THEBildeiz Yes, it's slower, but you don't have an option... unless if they have an API – MendelG Jun 29 '21 at 21:01