使用Python从JSON API中提取数据(喜欢) [英] Extract data (likes) from JSON API using Python

查看:185
本文介绍了使用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.com/walkers_crisps\\\
http://www.youtube.com/walkerscrisps',
u'has_added_app':False,
u' can_post':True,
u'cover':{
u'source':u'https://scontent.xx.fbcdn.net/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.com/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:\nhttp://twitter.com/walkers_crisps\nhttp://www.youtube.com/walkerscrisps',
    u'has_added_app': False,
    u'can_post': True,
    u'cover': {
      u'source': u'https://scontent.xx.fbcdn.net/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.com/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中提取数据(喜欢)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆