导航:首页 > 编程语言 > python提取json数据

python提取json数据

发布时间:2022-08-31 03:38:50

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组成。

这是用于序列化的两个模块:

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文档。

阅读全文

与python提取json数据相关的资料

热点内容
单片机自动控制 浏览:672
需要很多文件夹怎么快速的新建啊 浏览:67
算法申请着作权 浏览:213
以前手机号换了要怎么登录农行app 浏览:192
线切割编程系统怎么绘画 浏览:234
如何搭建云服务器异地容灾 浏览:923
黄金拐点指标源码 浏览:92
算法导论第九章 浏览:277
鸽子为什么生成服务器没反应 浏览:491
freebsdnginxphp 浏览:216
噪声消除算法 浏览:608
vue类似电脑文件夹展示 浏览:112
后备服务器有什么功效 浏览:269
连不上服务器怎么连 浏览:600
什么构架的可以刷安卓系统 浏览:771
爱奇艺APP怎么兑换CDK 浏览:994
程序员买4k显示器还是2k显示器 浏览:144
python多进程怎么多窗口 浏览:818
电脑文件夹怎么取消类别 浏览:47
cad拉线段命令 浏览:924