A. python 怎么提取json中的内容
python有json模块。json.loads将其转换为python对象即可
B. python怎么读取json文件内容
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
JSON在python中分别由list和dict组成。
这是用于序列化的两个模块:
json: 用于字符串和python数据类型间进行转换
pickle: 用于python特有的类型和python的数据类型间进行转换
Json模块提供了四个功能:mps、mp、loads、load
pickle模块提供了四个功能:mps、mp、loads、load
json mps把数据类型转换成字符串 mp把数据类型转换成字符串并存储在文件中 loads把字符串转换成数据类型 load把文件打开从字符串转换成数据类型
json是可以在不同语言之间交换数据的,而pickle只在python之间使用。json只能序列化最基本的数据类型,josn只能把常用的数据类型序列化(列表、字典、列表、字符串、数字、),比如日期格式、类对象!josn就不行了。而pickle可以序列化所有的数据类型,包括类,函数都可以序列化。
事例:
mps:将python中的 字典 转换为 字符串
C. python 怎么获取 json里的数据
#json string:
s = json.loads('{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}')
print s
print s.keys()
print s["name"]
print s["type"]["name"]
print s["type"]["parameter"][1]
D. Python如何从.json文件中获取数据
json是一个文本数据,读取进Python以后,可直接用eval函数解析文本成一个字典。或者可以用py自带的json包。json.load 或者json.loads方法,前面那个可以直接读文本文件,后面那个是读取字符串的。
E. Python 怎么获取json 里的特定的某个值
1、首先我们要导入json包,新建一个对象。
F. 如何在scrapy框架下用python爬取json文件
生成Request的时候与一般的网页是相同的,提交Request后scrapy就会下载相应的网页生成Response,这时只用解析response.body按照解析json的方法就可以提取数据了。代码示例如下(以京东为例,其中的parse_phone_price和parse_commnets是通过json提取的,省略部分代码):
# -*- coding: utf-8 -*-
from scrapy.spiders import Spider, CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from jdcom.items import JdPhoneCommentItem, JdPhoneItem
from scrapy import Request
from datetime import datetime
import json
import logging
import re
logger = logging.getLogger(__name__)
class JdPhoneSpider(CrawlSpider):
name = "jdPhoneSpider"
start_urls = ["http://list.jd.com/list.html?cat=9987,653,655"]
rules = (
Rule(
LinkExtractor(allow=r"list\.html\?cat\=9987,653,655\&page\=\d+\&trans\=1\&JL\=6_0_0"),
callback="parse_phone_url",
follow=True,
),
)
def parse_phone_url(self, response):
hrefs = response.xpath("//div[@id='plist']/ul/li/div/div[@class='p-name']/a/@href").extract()
phoneIDs = []
for href in hrefs:
phoneID = href[14:-5]
phoneIDs.append(phoneID)
commentsUrl = "http://sclub.jd.com/proctpage/p-%s-s-0-t-3-p-0.html" % phoneID
yield Request(commentsUrl, callback=self.parse_commnets)
def parse_phone_price(self, response):
phoneID = response.meta['phoneID']
meta = response.meta
priceStr = response.body.decode("gbk", "ignore")
priceJson = json.loads(priceStr)
price = float(priceJson[0]["p"])
meta['price'] = price
phoneUrl = "http://item.jd.com/%s.html" % phoneID
yield Request(phoneUrl, callback=self.parse_phone_info, meta=meta)
def parse_phone_info(self, response):
pass
def parse_commnets(self, response):
commentsItem = JdPhoneCommentItem()
commentsStr = response.body.decode("gbk", "ignore")
commentsJson = json.loads(commentsStr)
comments = commentsJson['comments']
for comment in comments:
commentsItem['commentId'] = comment['id']
commentsItem['guid'] = comment['guid']
commentsItem['content'] = comment['content']
commentsItem['referenceId'] = comment['referenceId']
# 2016-09-19 13:52:49 %Y-%m-%d %H:%M:%S
datetime.strptime(comment['referenceTime'], "%Y-%m-%d %H:%M:%S")
commentsItem['referenceTime'] = datetime.strptime(comment['referenceTime'], "%Y-%m-%d %H:%M:%S")
commentsItem['referenceName'] = comment['referenceName']
commentsItem['userProvince'] = comment['userProvince']
# commentsItem['userRegisterTime'] = datetime.strptime(comment['userRegisterTime'], "%Y-%m-%d %H:%M:%S")
commentsItem['userRegisterTime'] = comment.get('userRegisterTime')
commentsItem['nickname'] = comment['nickname']
commentsItem['userLevelName'] = comment['userLevelName']
commentsItem['userClientShow'] = comment['userClientShow']
commentsItem['proctColor'] = comment['proctColor']
# commentsItem['proctSize'] = comment['proctSize']
commentsItem['proctSize'] = comment.get("proctSize")
commentsItem['afterDays'] = int(comment['days'])
images = comment.get("images")
images_urls = ""
if images:
for image in images:
images_urls = image["imgUrl"] + ";"
commentsItem['imagesUrl'] = images_urls
yield commentsItem
commentCount = commentsJson["proctCommentSummary"]["commentCount"]
goodCommentsCount = commentsJson["proctCommentSummary"]["goodCount"]
goodCommentsRate = commentsJson["proctCommentSummary"]["goodRate"]
generalCommentsCount = commentsJson["proctCommentSummary"]["generalCount"]
generalCommentsRate = commentsJson["proctCommentSummary"]["generalRate"]
poorCommentsCount = commentsJson["proctCommentSummary"]["poorCount"]
poorCommentsRate = commentsJson["proctCommentSummary"]["poorRate"]
phoneID = commentsJson["proctCommentSummary"]["proctId"]
priceUrl = "http://p.3.cn/prices/mgets?skuIds=J_%s" % phoneID
meta = {
"phoneID": phoneID,
"commentCount": commentCount,
"goodCommentsCount": goodCommentsCount,
"goodCommentsRate": goodCommentsRate,
"generalCommentsCount": generalCommentsCount,
"generalCommentsRate": generalCommentsRate,
"poorCommentsCount": poorCommentsCount,
"poorCommentsRate": poorCommentsRate,
}
yield Request(priceUrl, callback=self.parse_phone_price, meta=meta)
pageNum = commentCount / 10 + 1
for i in range(pageNum):
commentsUrl = "http://sclub.jd.com/proctpage/p-%s-s-0-t-3-p-%d.html" % (phoneID, i)
yield Request(commentsUrl, callback=self.parse_commnets)
G. 如何用python读取json里面的值啊
1、首先需要在桌面新建‘json.txt’文件,内容为jsonline格式。
H. py 如何读取json的部分数据
result = json.loads(result_all)
json.loads 得到python对象,从你打印处理的结果看,返回的应该是字典对象
那么就可以使用字典的方法获得你想要的数据,如像下面这样:
result['trans_result '][0]['dst'] # 因为trans_result的value为列表所以要使用对应的索引值
希望能帮到你!
I. 如何用python读取json文件里指定的数据
importjson
withopen('who.json','r')asf:
data=json.load(f)
dependencies=data['dependencies']
fork,vindependencies.iteritems():
print(f'{k}@{v}')
J. 如何用Python,查找json格式中指定的数据,然后输出这些查找到的数据
用Python查找json格式中指定的数据输出这些查找到的数据的操作步骤如下:
1,打开一个编辑器,例如sublime text 3,然后创建一个新的PY文档。