- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我从 Instagram API 得到以下数据,我正在尝试获取文本 property
来自 caption
使用以下代码:
data = simplejson.load(info) # info is retrieved using the urllib2
for post in data['data']:
if post['caption'] is not "null":
try:
post['caption']['text']
except NameError:
post['caption']['text'] = 0
if post['caption']['text'] is not 0:
print post['caption']['text']
但我一直收到 TypeError: 'NoneType' object has no attribute '__getitem__'
错误 + UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: cha
打印 unicode 字符串时出错
racter maps to <undefined>
这是在 info
中检索和存储的 JSON 数据
{
"pagination":{
"next_url":"https:\/\/api.instagram.com\/v1\/users\/self\/feed?access_token=184046392.f59def8.c5726b469ad2462f85c7cea5f72083c0&count=3&max_id=247821697921944007_6064449",
"next_max_id":"247821697921944007_6064449"
},
"meta":{
"code":200
},
"data":[
{
"attribution":null,
"tags":[
"usausausa",
"olympics"
],
"type":"image",
"location":{
"latitude":37.785929,
"name":"Aquatech Swim School",
"longitude":-122.278718,
"id":16343815
},
"comments":{
"count":0,
"data":[
]
},
"filter":"Valencia",
"created_time":"1343765260",
"link":"http:\/\/instagr.am\/p\/NwhEktJvEp\/",
"likes":{
"count":0,
"data":[
]
},
"images":{
"low_resolution":{
"url":"http:\/\/distilleryimage1.s3.amazonaws.com\/61d9cbeedb4b11e1b8e822000a1e8b8e_6.jpg",
"width":306,
"height":306
},
"thumbnail":{
"url":"http:\/\/distilleryimage1.s3.amazonaws.com\/61d9cbeedb4b11e1b8e822000a1e8b8e_5.jpg",
"width":150,
"height":150
},
"standard_resolution":{
"url":"http:\/\/distilleryimage1.s3.amazonaws.com\/61d9cbeedb4b11e1b8e822000a1e8b8e_7.jpg",
"width":612,
"height":612
}
},
"caption":{
"created_time":"1343765325",
"text":"Part of my job to watch swimming. #olympics #USAUSAUSA",
"from":{
"username":"kissinkatkelly",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_4672491_75sq_1341713095.jpg",
"id":"4672491",
"full_name":"kissinkatkelly"
},
"id":"247843973390332239"
},
"user_has_liked":false,
"id":"247843429330383145_4672491",
"user":{
"username":"kissinkatkelly",
"website":"",
"bio":"I sing the body electric\r\n\r\nBay Area, CA",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_4672491_75sq_1341713095.jpg",
"full_name":"kissinkatkelly",
"id":"4672491"
}
},
{
"attribution":null,
"tags":[
],
"type":"image",
"location":{
"latitude":36.020832061,
"longitude":-121.548835754
},
"comments":{
"count":4,
"data":[
{
"created_time":"1343763343",
"text":"I wanna cut your mustache off. \ue313\ue313\ue313\ue004",
"from":{
"username":"glorias_noodles",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_24432017_75sq_1343633079.jpg",
"id":"24432017",
"full_name":"\ue340MeGusta Gloria\ue340"
},
"id":"247827343962686703"
},
{
"created_time":"1343763844",
"text":"Ahaha^",
"from":{
"username":"chloe_carter",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_44766575_75sq_1343509145.jpg",
"id":"44766575",
"full_name":"Chloe Carter"
},
"id":"247831551235474746"
},
{
"created_time":"1343763958",
"text":"Amazingg thoo",
"from":{
"username":"saulyp",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18051263_75sq_1335648741.jpg",
"id":"18051263",
"full_name":"Saul Perez"
},
"id":"247832506790200642"
},
{
"created_time":"1343764298",
"text":"@popesaintvictor where is that? :o",
"from":{
"username":"youknow_jameson",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_194001394_75sq_1343613135.jpg",
"id":"194001394",
"full_name":"Jameson Medina"
},
"id":"247835358103225704"
}
]
},
"filter":"Normal",
"created_time":"1343763202",
"link":"http:\/\/instagr.am\/p\/NwdJRpBkfX\/",
"likes":{
"count":611,
"data":[
{
"username":"jakyvedder",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18148021_75sq_1336938690.jpg",
"id":"18148021",
"full_name":"Janycken"
},
{
"username":"nadjasinbruker",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_174576513_75sq_1343582260.jpg",
"id":"174576513",
"full_name":"Nadja"
},
{
"username":"vivi11",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_1193390_75sq_1338169730.jpg",
"id":"1193390",
"full_name":"Viviana Rodriguez"
},
{
"username":"me_4_eva",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_181498114_75sq_1343506811.jpg",
"id":"181498114",
"full_name":"Kelly"
},
{
"username":"roxczajkowski",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_9367244_75sq_1343696914.jpg",
"id":"9367244",
"full_name":"Czajkowski \u041a\u043e\u0440\u0448\u0443\u043d\u043e\u0432\u0430"
},
{
"username":"arsi1989",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_201586134_75sq_1343761866.jpg",
"id":"201586134",
"full_name":"Arsalan MemOn"
},
{
"username":"puppyluva",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_201579504_75sq_1343760137.jpg",
"id":"201579504",
"full_name":"puppyluva"
},
{
"username":"paulinamurr",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_49364097_75sq_1343428499.jpg",
"id":"49364097",
"full_name":"Paulina Murray"
},
{
"username":"_mcquadeface_",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_20679753_75sq_1327617901.jpg",
"id":"20679753",
"full_name":"Emily McQuade"
}
]
},
"images":{
"low_resolution":{
"url":"http:\/\/distilleryimage11.s3.amazonaws.com\/96cd5b90db4611e1827612313814176c_6.jpg",
"width":306,
"height":306
},
"thumbnail":{
"url":"http:\/\/distilleryimage11.s3.amazonaws.com\/96cd5b90db4611e1827612313814176c_5.jpg",
"width":150,
"height":150
},
"standard_resolution":{
"url":"http:\/\/distilleryimage11.s3.amazonaws.com\/96cd5b90db4611e1827612313814176c_7.jpg",
"width":612,
"height":612
}
},
"caption":null,
"user_has_liked":false,
"id":"247826160271378391_605400",
"user":{
"username":"popesaintvictor",
"website":"http:\/\/popesaintvictor.com",
"bio":"artist, friend, and brand designer for blood:water mission in nashville, tennessee. \r\n\r\nhusband to @ohsoamy\r\n\r\nbe inspired. be awesome.\r\n",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_605400_75sq_1342893414.jpg",
"full_name":"pope saint victor",
"id":"605400"
}
},
{
"attribution":null,
"tags":[
],
"type":"image",
"location":{
"latitude":40.738834381,
"longitude":-73.994163513
},
"comments":{
"count":6,
"data":[
{
"created_time":"1343762733",
"text":"Nice :)",
"from":{
"username":"belieberpernille99",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_186196238_75sq_1341347304.jpg",
"id":"186196238",
"full_name":"official belieber"
},
"id":"247822232418879860"
},
{
"created_time":"1343762748",
"text":"Those pants \ud83d\ude0d",
"from":{
"username":"morganmarzulli",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_29155556_75sq_1337653621.jpg",
"id":"29155556",
"full_name":"morganmarzulli"
},
"id":"247822351461615990"
},
{
"created_time":"1343762777",
"text":"That outfit is to die for. I love her pants! They're so fun. \ud83d\udc4d",
"from":{
"username":"ninavnegron",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18421926_75sq_1343356820.jpg",
"id":"18421926",
"full_name":"Nina V"
},
"id":"247822600452278654"
},
{
"created_time":"1343762782",
"text":"YEAH THEIR COOL",
"from":{
"username":"belieberpernille99",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_186196238_75sq_1341347304.jpg",
"id":"186196238",
"full_name":"official belieber"
},
"id":"247822639375419775"
},
{
"created_time":"1343762782",
"text":"Another day another shoot! Look out for me and my chicest staff on @racked!",
"from":{
"username":"rebeccaminkoff",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6064449_75sq_1332274636.jpg",
"id":"6064449",
"full_name":"Rebecca Minkoff"
},
"id":"247822641497737600"
},
{
"created_time":"1343764430",
"text":"Hot mama! Miss you!",
"from":{
"username":"ashleekoston",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_12925089_75sq_1340081366.jpg",
"id":"12925089",
"full_name":"ashleekoston"
},
"id":"247836463642020446"
}
]
},
"filter":"Walden",
"created_time":"1343762670",
"link":"http:\/\/instagr.am\/p\/NwcIVwRYnH\/",
"likes":{
"count":528,
"data":[
{
"username":"claireyoung48",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_40091175_75sq_1338778945.jpg",
"id":"40091175",
"full_name":"claireyoung48"
},
{
"username":"l_christine_k",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_14871166_75sq_1341962995.jpg",
"id":"14871166",
"full_name":"Lauren Kawano"
},
{
"username":"grcdaly",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_41426567_75sq_1335023058.jpg",
"id":"41426567",
"full_name":"\u24bc\u24c7\u24b6\u24b8\u24ba \u24b9\u24b6\u24c1\u24e8"
},
{
"username":"vanessaalcalaa",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_18115905_75sq_1342828120.jpg",
"id":"18115905",
"full_name":"Vanessa Alcala"
},
{
"username":"makennalenover",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6464394_75sq_1343268613.jpg",
"id":"6464394",
"full_name":"Makenna Lenover"
},
{
"username":"heyitsmaryanne",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_623979_75sq_1340838647.jpg",
"id":"623979",
"full_name":"Maryanne L"
},
{
"username":"sarabeen",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/anonymousUser.jpg",
"id":"6463387",
"full_name":"sarabeen"
},
{
"username":"boldincrimson",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_191122242_75sq_1341889110.jpg",
"id":"191122242",
"full_name":"Pilar Chapa"
},
{
"username":"elizzabethhope",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6761399_75sq_1342553102.jpg",
"id":"6761399",
"full_name":"Lizz\u270c"
}
]
},
"images":{
"low_resolution":{
"url":"http:\/\/distilleryimage7.s3.amazonaws.com\/59bc708edb4511e1b7ea22000a1cbb16_6.jpg",
"width":306,
"height":306
},
"thumbnail":{
"url":"http:\/\/distilleryimage7.s3.amazonaws.com\/59bc708edb4511e1b7ea22000a1cbb16_5.jpg",
"width":150,
"height":150
},
"standard_resolution":{
"url":"http:\/\/distilleryimage7.s3.amazonaws.com\/59bc708edb4511e1b7ea22000a1cbb16_7.jpg",
"width":612,
"height":612
}
},
"caption":null,
"user_has_liked":false,
"id":"247821697921944007_6064449",
"user":{
"username":"rebeccaminkoff",
"website":"http:\/\/www.rebeccaminkoff.com",
"bio":"The Downtown Romantic. My life, my work, my world.",
"profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_6064449_75sq_1332274636.jpg",
"full_name":"Rebecca Minkoff",
"id":"6064449"
}
}
]
}
最佳答案
您不需要对帖子标题是否存在“文本”进行复杂的测试。此代码适用于您发布的 JSON 字符串:
for post in data['data']:
if post.get('caption'):
print post['caption'].get('text', 0)
此外,您可以更具防御性并在开始循环时引用 data.get('data', [])
以防 Instagram 向您发送空 JSON。
关于python - 检查JSON(对象属性存在)是否打印为unicode解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11748234/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!