Reputation: 45
I am trying to create a program that would text me anytime ethereum prices change by a decent amount. In order to do this i have a while loop continuously parsing and grabbing the information. However it will give me the information three times and then give me the error:
change = json.loads(soup.select_one('script#server-app-state').contents[0])
AttributeError: 'NoneType' object has no attribute 'contents'
My code:
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,'%')
Upvotes: 1
Views: 73
Reputation: 195458
You are getting a "CAPTCHA" page because you're making too many requests too quickly. Put time.sleep()
in your loop and try..except
around the json.loads
.
For example:
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,'%')
Prints:
-2.21 %
-2.21 %
-2.21 %
-
-
-
-
-2.21 %
-2.21 %
-2.21 %
-
... and so on.
Upvotes: 1