在Python,有什么之间的差异urllib
,urllib2
以及urllib3
模块?为什么有三个?他们似乎做同样的事情......
我有点头疼,因为一个简单的外观,简单的陈述在我脸上抛出一些错误.
我有一个名为strings.json的json文件,如下所示:
"strings": [{"-name": "city", "#text": "City"}, {"-name": "phone", "#text": "Phone"}, ...,
{"-name": "address", "#text": "Address"}]
Run Code Online (Sandbox Code Playgroud)
我现在想读取json文件.我发现了这些陈述,但它不起作用:
import json
from pprint import pprint
with open('strings.json') as json_data:
d = json.load(json_data)
json_data.close()
pprint(d)
Run Code Online (Sandbox Code Playgroud)
控制台上显示的错误是:
Traceback (most recent call last):
File "/home/.../android/values/manipulate_json.py", line 5, in <module>
d = json.loads(json_data)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
[Finished in 0.1s with exit code 1]
Run Code Online (Sandbox Code Playgroud)
编辑 …
我的项目当前正在python中收到一条JSON消息,我需要从中获取一些信息.出于此目的,我们将其设置为字符串中的一些简单JSON:
jsonStr = '{"one" : "1", "two" : "2", "three" : "3"}'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在使用列表生成JSON请求,json.dumps
但是为了做到与此相反,我认为我需要使用它,json.loads
但我没有太多运气.任何人都可以给我一个片段,在上面的例子中输入"2"会返回"2"吗?
所以我花了很多时间在这上面,在我看来它应该是一个简单的修复.我正在尝试使用Facebook的身份验证来注册我的网站上的用户,而我正试图在服务器端进行操作.我已经到了获得访问令牌的地步,当我去的时候:
https://graph.facebook.com/me?access_token=MY_ACCESS_TOKEN
我得到的信息是我正在寻找的字符串,如下所示:
{"id":"123456789","name":"John Doe","first_name":"John","last_name":"Doe","link":"http:\/\/www.facebook.com\/jdoe","gender":"male","email":"jdoe\u0040gmail.com","timezone":-7,"locale":"en_US","verified":true,"updated_time":"2011-01-12T02:43:35+0000"}
看起来我应该能够使用dict(string)
这个,但我收到这个错误:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
所以我尝试使用Pickle,但得到了这个错误:
KeyError: '{'
我尝试使用django.serializers
反序列化它,但有类似的结果.有什么想法吗?我觉得答案必须简单,我只是愚蠢.谢谢你的帮助!
我如何用python解析json api响应?我目前有这个:
import urllib.request
import json
url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'
def response(url):
with urllib.request.urlopen(url) as response:
return response.read()
res = response(url)
print(json.loads(res))
Run Code Online (Sandbox Code Playgroud)
我收到此错误:TypeError:JSON对象必须是str,而不是'bytes'
什么是pythonic方式来处理json apis?
我试图从api调用api.trends()[Tweepy]返回的JSON对象中提取数据,但我无法提取数据.
任何人都可以给我一个如何从JSON对象中提取数据的示例.我想以表格形式提取数据.
提前致谢.
我收到以下错误消息:
...文件“c:\users\dockerhost\appdata\local\programs\python\python37\Lib\json\decoder.py”,第 355 行,raw_decode 引发 JSONDecodeError("Expecting value", s, err.value)从无 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我正在尝试使用请求来获取响应标头。我在尝试解决 jsondecodeerror 方面做了很多研究。但是,我没有找到解决方案。
import requests
request.get('https://www.google.com/').json()
Error message.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Host\.virtualenvs\projects08-8iyGSYl4\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "c:\users\host\appdata\local\programs\python\python37\Lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "c:\users\host\appdata\local\programs\python\python37\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "c:\users\host\appdata\local\programs\python\python37\Lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: …
Run Code Online (Sandbox Code Playgroud) 我现在已经环顾了几天,无法解决这个问题.基本上我是将图像上传到服务器并获得一个ID作为回报,问题是我无法弄清楚如何提取此ID并将其更改为准备保存到数据库中的String.
程序代码
url = <Server address>
with open("image.jpg", "rb") as image_file:
files = {'file': image_file}
auth = ('<Key>', '<Pass>')
r = requests.post(url, files=files, auth=auth)
data = r.json()
uploaded = data.get('uploaded')
content_id = uploaded[0]
print r
print r.text
print '--------------'
print str(content_id)
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出
<Response [200]>
{
"status": "success",
"uploaded": [
{
"filename": "image.jpg",
"id": "6476edfa1d262ad81181d992da78149d"
}
]
}
--------------
{u'id': u'6476edfa1d262ad81181d992da78149d', u'filename': u'image.jpg'}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个 Python 网络抓取工具(个人使用)并且在检索 JSON 文件时遇到了一些麻烦。我能够找到我需要的请求 URL,但是当我运行我的脚本(我正在使用请求)时,URL 返回 HTML 而不是 Chrome 开发人员工具控制台中显示的 JSON。这是我当前的脚本:
import requests
import json
url = 'https://nytimes.wd5.myworkdayjobs.com/Video?clientRequestID=1f1a6071627946499b4b09fd0f668ef0'
r = requests.get(url)
print(r.text)
Run Code Online (Sandbox Code Playgroud)
对 Python 来说是全新的,因此非常感谢任何朝正确方向的推动。谢谢!
python ×9
json ×7
python-3.x ×2
api ×1
dictionary ×1
facebook ×1
parsing ×1
python-2.x ×1
simplejson ×1
string ×1
tweepy ×1
urllib ×1
urllib2 ×1