導航:首頁 > 編程語言 > 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數據相關的資料

熱點內容
帝國神話組建雲伺服器 瀏覽:825
鄧散木pdf 瀏覽:197
方舟怎麼直連伺服器圖片教程 瀏覽:561
假相pdf 瀏覽:334
找對象找程序員怎麼找 瀏覽:976
怎麼投訴蘋果商店app 瀏覽:470
華為手機如何看有多少個app 瀏覽:734
btr如何管理別的伺服器 瀏覽:410
spwm軟體演算法 瀏覽:184
70多歲單身程序員 瀏覽:221
高考考前解壓拓展訓練 瀏覽:217
用紙做解壓玩具不用澆水 瀏覽:584
谷輪壓縮機序列號 瀏覽:736
牛頓插值法編程 瀏覽:366
php多用戶留言系統 瀏覽:731
安卓和蘋果如何切換流量 瀏覽:703
怎麼知道dns伺服器是多少 瀏覽:976
5995用什麼簡便演算法脫式計算 瀏覽:918
電腦上如何上小米雲伺服器地址 瀏覽:921
手機資料解壓密碼 瀏覽:444