Reputation: 764
I am getting "invalid character in identifier" error, for the below code. The 'http' in line 3 gets highlighted when the error is shown. I am new to Python, please help.
import requests
import html
r = requests.get(“http://cricapi.com/api/cricket”)
if r.status_code == 200:
currentMatches = r.json()[“data”]
for match in currentMatches:
print(html.unescape(match[“title”]))
else:
print(“Error in retrieving the current cricket matches”)
Upvotes: 2
Views: 18380
Reputation: 41
The seems to be the double quotes you used, you used “
which is different then "
.
This means that python didn't know it is a string, and this is why you got a syntax error.
This can be a problem with your text editor, i would recommand using sublime text or npp.
And another thing, what you are trying to do probably won't work, since if there's an error retriving the matches, you can't be sure the dict will have the key "data"
.
I would recommand using try-except to know if the response contains any data, or using
d = r.json()
if "data" in d:
print "Got data"
else:
print "Error getting data"
Upvotes: 4