[英]Python3 requests always returning same request
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.
我正在尝试使用标题。
Well, you're doing:好吧,你正在做:
while(True):
...
which means that you are forever going to loop over coin_list
since this condition is always True
.这意味着您将永远遍历
coin_list
因为此条件始终为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
:因此,要解决此问题,只需删除
while
条件,然后循环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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.