简体   繁体   English

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

[英]Trying to get json data from URL using Python

I am learning to get json data from a link and use that data later on. 我正在学习从链接获取json数据,并在以后使用该数据。 But i am getting error: "RuntimeError: maximum recursion depth exceeded while calling a Python object" 但是我收到错误消息:“ RuntimeError:调用Python对象时超出了最大递归深度”

Here is my code: 这是我的代码:

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)

EDIT: Okay I got the response data from using this slight change, here is the new code: 编辑:好的,我从使用此轻微更改得到了响应数据,这是新代码:

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())

Don't use beautiful soup to process a json http response. 不要使用漂亮的汤来处理json http响应。 Use something like requests: 使用类似请求的内容:

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)

The beautiful soup object can't be parsed with json.loads() that way. 这样就无法用json.loads()解析漂亮的汤对象。

If you have HTML data on some of those json keys then you can use beautiful soup to parse those string values individually. 如果您在某些json键上具有HTML数据,则可以使用漂亮的汤单独解析那些字符串值。 If you have a key called content on your json, containing html, you can parse it like so: 如果您在包含html的json上有一个名为content的键,则可以这样解析它:

BeautifulSoup(newDictionary.content, "lxml")

You may need to experiment with different parsers, if you have fragmentary html. 如果您有不完整的html,则可能需要尝试使用其他解析器。

The following is an example of how to use various JSON data that has been loaded as an object with json.loads() . 以下是如何使用已通过json.loads()作为对象加载的各种JSON数据的示例。

Working Example — Tested with Python 2.6.9 and 2.7.10 and 3.3.5 and 3.5.0 工作示例 —已在Python 2.6.92.7.10以及3.3.53.5.0中进行了测试

import json

json_data = '''
{
    "array": [
        1,
        2,
        3
    ],
    "boolean": true,
    "null": null,
    "number": 123,
    "object": {
        "a": "b",
        "c": "d",
        "e": "f"
    },
    "string": "Hello World"
}
'''

data = json.loads(json_data)

list_0 = [
    data['array'][0],
    data['array'][1],
    data['array'][2],
    data['boolean'],
    data['null'],
    data['number'],
    data['object']['a'],
    data['object']['c'],
    data['object']['e'],
    data['string']
]

print('''
array value 0           {0}
array value 1           {1}
array value 2           {2}
boolean value           {3}
null value              {4}
number value            {5}
object value a value    {6}
object value c value    {7}
object value e value    {8}
string value            {9}
'''.format(*list_0))

Output 产量

array value 0           1
array value 1           2
array value 2           3
boolean value           True
null value              None
number value            123
object value a value    b
object value c value    d
object value e value    f
string value            Hello World

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM