我正在学习从链接获取 json 数据并稍后使用该数据。但我收到错误:“RuntimeError:调用 Python 对象时超出了最大递归深度”
这是我的代码:
import json
import requests
from bs4 import BeautifulSoup
url = "http://example.com/category/page=2&YII_CSRF_TOKEN=31eb0a5d28f4dde909d3233b5a0c23bd03348f69&more_products=true"
header = {'x-requested-with': 'XMLHttpRequest'}
mainPage = requests.get(url, headers = header)
xTree = BeautifulSoup(mainPage.content, "lxml")
newDictionary=json.loads(str(xTree))
print (newDictionary)
编辑:好的,我通过使用这个微小的更改获得了响应数据,这是新代码:
import json
import requests
from bs4 import BeautifulSoup
url = "http://example.com/category/page=2&YII_CSRF_TOKEN=31eb0a5d28f4dde909d3233b5a0c23bd03348f69&more_products=true"
header = {'x-requested-with': 'XMLHttpRequest'}
mainPage = requests.get(url, headers = header
print (mainPage.json())
最佳答案
不要使用 beautiful soup 来处理 json http 响应。使用请求之类的东西:
url = "https://www.daraz.pk/womens-kurtas-shalwar-kameez/?pathInfo=womens-kurtas-shalwar-kameez&page=2&YII_CSRF_TOKEN=31eb0a5d28f4dde909d3233b5a0c23bd03348f69&more_products=true"
header = {'x-requested-with': 'XMLHttpRequest'}
t = requests.get(url, headers=True)
newDictionary=json.loads(t)
print (newDictionary)
美丽的汤对象无法用 json.loads() 这样解析。
如果您在其中一些 json 键上有 HTML 数据,那么您可以使用 beautiful soup 单独解析这些字符串值。如果你的 json 中有一个名为 content 的键,包含 html,你可以像这样解析它:
BeautifulSoup(newDictionary.content, "lxml")
如果您有零碎的 html,您可能需要尝试不同的解析器。
关于python - 尝试使用Python从URL获取json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102330/