python - 尝试使用Python从URL获取json数据

标签 python json

我正在学习从链接获取 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/

相关文章:

python - 如何使用 Base64 字符串或字节数组设置 QLabel

json - 运算符 '.' 无法应用于 lambda 表达式

jquery - 如何让 .getJson 成功

python - 在Python中对列表进行切片

python - 在 python 中结合两个势

python - 区分人名和公司名称列表

python - 按前 n 行对二维数组进行排序

javascript - 在 .getJson() 中传递同一参数的多个值

javascript - 来自 JSON 对象的动态 Knockout 表

javascript - file_put_content 数组结构不起作用