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