使用 Python 从 JSON API 中提取数据(喜欢)
Extract data (likes) from JSON API using Python
我想为我的项目拉取点赞数。
这是我的代码:
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
这是输出:
{
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
}
我想拉点赞数,最好只拉点数。一个人会怎么做呢?
由于这是一个生成器,因此没有好的 方法来获取特定元素。
您可以遍历它并检查每个元素是否是您要查找的元素,或者,如果您需要其中的多个元素,则将其转换为字典。
这是一种转换方法:
new_dictionary = {}
for name, value in datas:
new_dictionary[name] = value
有了这个你就可以得到喜欢:
likes = new_dictionary['likes']
或者,如果您只想从中获取 'items':
for name, value in datas:
if name == 'likes':
likes = value
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)
likes = datas['likes']
print likes
我想为我的项目拉取点赞数。
这是我的代码:
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
这是输出:
{
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
}
我想拉点赞数,最好只拉点数。一个人会怎么做呢?
由于这是一个生成器,因此没有好的 方法来获取特定元素。 您可以遍历它并检查每个元素是否是您要查找的元素,或者,如果您需要其中的多个元素,则将其转换为字典。
这是一种转换方法:
new_dictionary = {}
for name, value in datas:
new_dictionary[name] = value
有了这个你就可以得到喜欢:
likes = new_dictionary['likes']
或者,如果您只想从中获取 'items':
for name, value in datas:
if name == 'likes':
likes = value
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)
likes = datas['likes']
print likes