导航:首页 > 编程语言 > 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数据相关的资料

热点内容
0855手机版免费观看 浏览:455
pdf运行 浏览:226
中国加密市场目标 浏览:922
看着就很舒服的推拿解压 浏览:374
免费无广告的电影app 浏览:823
程序员之家打开方式 浏览:845
4k蓝光电影下载网站 浏览:896
罗丽克莱尔演的电影 浏览:692
免费刺激的电影有哪些条件 浏览:506
女老师和男生谈恋爱电影推荐 浏览:199
任天堂如何更改服务器账号 浏览:17
排解压力大法教学视频 浏览:156
血咒电影 浏览:497
r6应该玩什么服务器 浏览:767
小电影都是在哪儿找到的 浏览:9
免费电影电视剧在线观看网站 浏览:864
华为手机如何设置安装app 浏览:457
程序员大会小品 浏览:339
sw协作服务器怎么关闭 浏览:635
西安日立压缩机有限公司 浏览:549