使用Python从JSON API中提取数据(喜欢)

编程入门 行业动态 更新时间:2024-10-27 23:21:36
本文介绍了使用Python从JSON API中提取数据(喜欢)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想拉我的项目的喜欢数量。

这是我的代码:

import facepy from从BS4进口BeautifulSoup 进口JSON 存取= 'CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf6tPaf7HsPF3J9DYRWi3YuSTf0HXQwr2LMAgczDBWBSDNFzHrEjxzkBQ9hbZCYC1fB2z1qyHs5BeAZCV3zyU8JhEcbSiiB5Bf73gZAfQ1rUa2pdx9U24dUZCX0qMDzvXHLHV9jPRiZBByB2b2uEHGk22M4ZD' 图表= GraphAPI(访问) PAGE_ID = '步行者' DATAS facepy进口GraphAPI = graph.get(page_id +'/',page = True,retry = 5)数据中的数据:打印数据

这里是输出:

{ u'category :u'Product / Service', u'username':u'walkers', u'about':u欢迎来到Walkers Crisps的家,当谈到让Brits微笑的时候,我们在这里,周一到周五上午9点到下午6点!, u'talking_about_count':3076, u'description':你要找详细了解Walkers,请访问:\\\http://twitter/walkers_crisps\\\http://www.youtube/walkerscrisps', u'has_added_app':False, u' can_post':True, u'cover':{ u'source':u'https://scontent.xx.fbcdn/hphotos-xpt1/t31.0-8/s720x720/ 11165156_10153204315777649_4115137634691483959_o.jpg', u'cover_id':u'10153204315777649', u'offset_x':0, u'offset_y':0, u'id': u'10153204315777649'}, u'name':u'Walkers', u'website':u'http://www.walkers.co.uk', u'link':u'https://www.facebook/walkers', u'likes':552762, u'parking':{ u'street ':0, u'lot':0, u'valet':0 }, u'is_community_page':False, u'were_here_count' :0, u'checkins':0, u'id':u'53198517648', u'is_published':True }

我想拉出喜欢的数量只是数字。怎么会这样做?

解决方案

因为这是一个生成器,所以没有一个很好的方式来获取一个特定的元素。 您可以遍历它,并检查每个元素是否正在寻找,或者如果您需要多个元素,请将其转换为字典。

这是一种转换方式:

new_dictionary = {} 名称,数据中的值: new_dictionary [name] = value

得到喜欢:

likes = new_dictionary ['likes']

或者如果您只想从中获取项目:

名称,数据中的值:如果name =='喜欢': likes = value

I want to pull the number of likes for my project.

Here's my code:

import facepy from facepy import GraphAPI from bs4 import BeautifulSoup import json access = 'CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf6tPaf7HsPF3J9DYRWi3YuSTf0HXQwr2LMAgczDBWBSDNFzHrEjxzkBQ9hbZCYC1fB2z1qyHs5BeAZCV3zyU8JhEcbSiiB5Bf73gZAfQ1rUa2pdx9U24dUZCX0qMDzvXHLHV9jPRiZBByB2b2uEHGk22M4ZD' graph = GraphAPI(access) page_id= 'walkers' datas= graph.get(page_id+'/', page=True, retry=5) for data in datas: print data

And here's the output:

{ u'category': u'Product/Service', u'username': u'walkers', u'about': u"Welcome to the home of Walkers Crisps. When it comes to making Brits smile, we\u2019ve got it in the bag (yeah, we went there.) We're here Mon-Fri, 9am-6pm!", u'talking_about_count': 3076, u'description': u'To find out more about Walkers, visit:\ntwitter/walkers_crisps\nwww.youtube/walkerscrisps', u'has_added_app': False, u'can_post': True, u'cover': { u'source': u'scontent.xx.fbcdn/hphotos-xpt1/t31.0-8/s720x720/11165156_10153204315777649_4115137634691483959_o.jpg', u'cover_id': u'10153204315777649', u'offset_x': 0, u'offset_y': 0, u'id': u'10153204315777649' }, u'name': u'Walkers', u'website': u'www.walkers.co.uk', u'link': u'www.facebook/walkers', u'likes': 552762, u'parking': { u'street': 0, u'lot': 0, u'valet': 0 }, u'is_community_page': False, u'were_here_count': 0, u'checkins': 0, u'id': u'53198517648', u'is_published': True }

I want to pull the number of likes, preferably just the number. How would one go about doing this?

解决方案

As this is a generator, there's no good way to get a specific element. You can either iterate over it and check for each element if it's the one you're looking for, or, if you will need more than one element from it, convert it into a dictionary.

This is one way to convert it:

new_dictionary = {} for name, value in datas: new_dictionary[name] = value

With this you could then get likes with:

likes = new_dictionary['likes']

Or if you only want to get 'items' from it:

for name, value in datas: if name == 'likes': likes = value

更多推荐

使用Python从JSON API中提取数据(喜欢)

本文发布于:2023-10-31 08:04:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:喜欢   数据   Python   JSON   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!