我正在尝试创建一个程序,以便在以太坊价格出现较大幅度的变化时向我发送短信。为了做到这一点,我有一个while循环不断地解析和获取信息。但是,它将给我三遍信息,然后给我错误:
change = json.loads(soup.select_one('script#server-app-state').contents[0])
AttributeError: 'NoneType' object has no attribute 'contents'
我的代码:
import json
import time
import requests
from bs4 import BeautifulSoup
normalprice = True
URL = 'https://www.coinbase.com/price/ethereum'
while normalprice:
soup = BeautifulSoup(requests.get(URL).content, "html.parser")
change = json.loads(soup.select_one('script#server-app-state').contents[0])
BDP = change['initialData']['data']['prices']['prices']['latestPrice']['percentChange']['day']
BRV = round(BDP * 100, 2)
print (BRV,'%')
您会收到一个“ CAPTCHA”页面,因为您发出的请求太快了。放入time.sleep()
您的循环和try..except
周围json.loads
。
例如:
import json
import time
import requests
from bs4 import BeautifulSoup
normalprice = True
URL = 'https://www.coinbase.com/price/ethereum'
while normalprice:
time.sleep(3)
soup = BeautifulSoup(requests.get(URL).content, "html.parser")
try:
change = json.loads(soup.select_one('script#server-app-state').contents[0])
except:
print('-')
continue
BDP = change['initialData']['data']['prices']['prices']['latestPrice']['percentChange']['day']
BRV = round(BDP * 100, 2)
print (BRV,'%')
印刷品:
-2.21 %
-2.21 %
-2.21 %
-
-
-
-
-2.21 %
-2.21 %
-2.21 %
-
... and so on.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句