Extract data (likes) from JSON API using Python

2024/10/16 3:20:50

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?

Answer

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
https://en.xdnf.cn/q/117755.html

Related Q&A

No nested nodes. How to get one piece of information and then to get additional info respectively?

For the code below I need to get dates and their times+hrefs+formats+...(not shown) respectively.<div class="showtimes"><h2>The Little Prince</h2><div class="poster&…

I need help changing the color of text in python

Hey I need help with coloring the text in a program I am making. It is a password program and I am trying to make the denied and granted red and green when they appear. Here is the program so far:passw…

How do I use Liclipse to write a ParaView script?

Ive tried following the directions here without success. Here are some of my environment variables:Path: C:\Python34\;C:\Python34\Scripts;...;C:\Program Files (x86)\ParaView 4.3.1\lib\paraview-4.3\site…

List of tuples to nested dictionary without overriding

I need to convert the above list of tuples to nested dictionary without overwriting the value as below in python[(a, 1),(b, true),(b, none),(a, 2),(b, true),(a, 3),(b, false)]{a: {1 : { b : (true,none)…

Rotate matplotlib pyplot with curve by 90 degrees

I have plot with one line as this:import numpy as np import matplotlib.pyplot as pla = np.array([4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9]) b = np.array([i/len(a) for i in range(1,…

Reading csv file and returning as dictionary

Ive written a function that currently reads a file correctly but there are a couple of problems. It needs to be returned as a dictionary where the keys are artist names and the values are lists of tupl…

Spark converting Pandas df to S3

Currently i am using Spark along with Pandas framework. How can I convert Pandas Dataframe in a convenient way which can be written to s3. I have tried below option but I get error as df is Pandas dat…

install jupyter notebook in windows

My Python version is 3.6.0 and my operating system is Windows. I want to install jupyter notebook using the order pip install jupyter. But it failed, I got the following error:

Play a sound using python subprocess and threading

I am trying to open an alert, then loop a sound until the alert is closed. Then the sound should stop.I tried this:import threading import time import subprocessstop_sound = False def play_alarm(file_n…

Referencing `self` in `__old__` in PyContract constraints

Im working on writing some constraints for a class method using PyContract (not PyContracts). As a postcondition, Id like to ensure that the memory address of the instance hasnt changed i.e. id(self) s…