【发布时间】:2020-10-04 07:37:12
【问题描述】:
我正在尝试创建一个程序,只要以太坊价格发生相当大的变化,它就会给我发短信。为了做到这一点,我有一个 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,'%')
【问题讨论】:
-
因为当时
select_one('script#server-app-state')返回了None,并且在None 上调用.contents引发了异常。
标签: python json parsing beautifulsoup