您好我尝试使用requests.get
解析profile而使用字符串解析来解析ElementTree,并且它一直告诉我它是一个错误的xml文件xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
。我从请求中获取api的响应,我尝试使用不同的解析器。
以下是我的尝试:
import requests
from xml.etree import ElementTree
url = 'https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/52456930/summary?season=SEASON2016&api_key=2c0543ab-aeca-476d-8276-a5c23748898c'
response = requests.get(url)
xmlstring = response.content # or response.text i dont know
parser = ElementTree.XMLParser(encoding="utf-8")
ElementTree.fromstring(xmlstring, parser=parser)`
我需要解析它以便从中询问特定的变量。
答案 0 :(得分:0)
您可以使用python json
库将其解析为普通的python字典。
import requests, json
url = 'https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/52456930/summary?season=SEASON2016&api_key=2c0543ab-aeca-476d-8276-a5c23748898c'
response = requests.get(url)
data = json.loads(response.content)
for stat in data['playerStatSummaries']:
print stat['wins']
<强>输出强>
14
143
21
1
0
0
8
36
545