I have a simple question in python.
If I have a Facebook page ID let say '6127898346' for example, how can I retrieve this page information such as (Likes count) , and store the result on a file?
Use some sort of Facebook API package, like https://github.com/pythonforfacebook/facebook-sdk available with pip install facebook-sdk.
import facebook
graph = facebook.GraphAPI()
page = graph.get_object('6127898346')
print '{} has {} likes.'.format(page['name'], page['likes'])
Easy way to save everything:
import json
with open('outf.json', 'w') as f:
json.dump(page, f)
Facebook has updated their API and now the same requests would need access token. The graph API is also changed and to get likes count, you can do following:
def get_fb_page_like_count(fb_page_id)
graph = facebook.GraphAPI(access_token=your_access_token)
args = {'fields': 'likes'}
page = graph.get_object(fb_page_id, **args)
return page.get('likes', 0)
Related
http://www.youtube.com/dev/ ive already checked the youtube api dev info and have not found anything pertaining to this.
Obtain API youtube
To be able to make requests of this type you must have an API key,
Go to link https://console.developers.google.com/
Create a new project
find youtube Data API v3 and click on it
Enable the API
go to credentials and create one for the project
write it down and insert it in the script below
This script uses ÁPI created previously to make requests on the channels by creating name in a random way and inserts the data in two files in the first it stores all the info in the second only the id, the channel name and the link to the channel, I hope it is what you are looking for ;)
import json
import urllib.request
import string
import random
channels_to_extract = 100
API_KEY = '' #your api key
while True:
random_name = ''.join(random.choice(string.ascii_uppercase) for _ in range(random.randint(3,10))) # for random name of channel to search
urlData = "https://www.googleapis.com/youtube/v3/search?key={}&maxResults={}&part=snippet&type=channel&q={}".format(API_KEY,channels_to_extract,random_name)
webURL = urllib.request.urlopen(urlData)
data = webURL.read()
encoding = webURL.info().get_content_charset('utf-8')
results = json.loads(data.decode(encoding))
results_id={}
if results['pageInfo']["totalResults"]>=channels_to_extract: # may return 0 result because is a random name
break # when find a result break
for result in results['items']:
results_id[result['id']['channelId']]=[result["snippet"]["title"],'https://www.youtube.com/channel/'+result['id']['channelId']] # get id and link of channel for all result
with open("all_info_channels.json","w") as f: # write all info result in a file json
json.dump(results,f,indent=4)
with open("only_id_channels.json","w") as f: # write only id of channels result in a file json
json.dump(results_id,f,indent=4)
for channelId in results_id.keys():
print('Link --> https://www.youtube.com/channel/'+channelId) # link at youtube channel for all result
I want to scrape the data from Instagram different public and private accounts. But when I run the following code.
from instagramy import Instagram
# Connecting the profile
user = Instagram("ihtishamKhattak")
# printing the basic details like
# followers, following, bio
print(user.is_verified())
print(user.popularity())
print(user.get_biography())
# return list of dicts
posts = user.get_posts_details()
print('\n\nLikes', 'Comments')
for post in posts:
likes = post["likes"]
comments = post["comment"]
print(likes,comments)
You can have a look at the instagramy pypi page.
There is no Instagram python module. Also make sure, that you have no script calles instagramy.py
change your script to
from instagramy import InstagramUser
# Connecting the profile
user = InstagramUser("ihtishamKhattak")
Beware that this might work only for few request if you do not provide a Session ID.
I am trying to download all the posts and the comments(and replies to comments) for that post in a public facebook page. Here is the code that I am using:
from facepy import GraphAPI
import json
page_id = "Google"
access_token = "access_token"
graph = GraphAPI(access_token)
data = graph.get(page_id + "/feed", page=True, retry=3, limit=100,fields='message,likes')
i = 0
for p in data:
print 'Downloading posts', i
with open('facepydata/content%i.json' % i, 'w') as outfile:
json.dump(p, outfile, indent = 4)
i += 1
First of all (1) this code is giving me this exception:
facepy.exceptions.FacebookError: [1] Please reduce the amount of data you're asking for, then retry your request
How should I solve this problem?
Second: (2)How can I get all the likes,comments and replies at the same time of getting the posts (paging is also required in likes,comments and replies to get all of them). page=True not working for these fields.
Thank you!
Facebooks Graph API has rate limiting. I believe #Klaus-D is correct that the error it is clear the request should have a lower limit parameter set, in which you can then page through the results of.
I would try limit=10, and then page through with your loop as you have it.
I am new to Facebook API. Currently, I am trying to print out ALL the comments that have been posted for this facebook page called 'leehsienloong'. However, I could only print out a total of 700+ comments. I'm sure there are more than 700+ comments in total.
I find out that the problem is, I did not request to go to another page to print out the comments. I read about paging Facebook API, but I still do not understand how to do the code for paging.
Is there anyone out there who will be able to help/assist me? I really need help. Thank you.
Here is my code, without paging:
import facebook #sudo pip install facebook-sdk
import itertools
import json
import re
import requests
access_token = "XXX"
user = 'leehsienloong'
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')
Jstr = json.dumps(posts)
JDict = json.loads(Jstr)
count = 0
for i in JDict['data']:
allID = i['id']
try:
allComments = i['comments']
for a in allComments['data']:
count += 1
print a['message']
except (UnicodeEncodeError):
pass
print count
You can use the limit parameter to increase the number of comments to be fetched. The default is 25. You can increase it like this:
posts = graph.get_connections(profile['id'], 'posts', limit=100)
But more convenient way would be get the previous and next pages from paging and do multiple requests.
to get all the comments of a post the logic should be something like
comments = []
for post in posts["data"]:
first_comments = graph.get_connections(id=post["id"], connection_name="comments")
comments.extend(first_comments["data"])
while True:
try:
next_comments = requests.get(post_comments["paging"]["next"]).json()
comments.extend(next_comments["data"])
except KeyError:
break
I'm trying to crawl Youtube to retrieve information about a group of users (approx. 200 people). I'm interested in looking for relationships between the users:
contacts
subscribers
subscriptions
what videos they commented on
etc
I've managed to get contact information with the following source:
import gdata.youtube
import gdata.youtube.service
from gdata.service import RequestError
from pub_author import KEY, NAME_REGEX
def get_details(name):
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = KEY
contact_feed = yt_service.GetYouTubeContactFeed(username=name)
contacts = [ e.title.text for e in contact_feed.entry ]
return contacts
I can't seem the get the other bits of information I need. The reference guide says that I can grab the XML feed from http://gdata.youtube.com/feeds/api/users/username/subscriptions?v=2 (for some arbitrary user). However, if I try to get other users' subscriptions, I get the a 403 error with the following message:
User must be logged in to access these subscriptions.
If I use the gdata API:
sub_feed = yt_service.GetYouTubeSubscriptionFeed(username=name)
sub = [ e.title.text for e in contact_feed.entry ]
then I get the same error.
How can I get these subscriptions without logging in? It should be possible, as you can access this information without logging in to the Youtube web-site.
Also, there seems to be no feed for the subscribers of particular user. Is this information available through the API?
EDIT
So, it appears this can't be done through the API. I had to do this the quick and dirty way:
for f in `cat users.txt`; do wget "www.youtube.com/profile?user=$f&view=subscriptions" --output-document subscriptions/$f.html; done
Then use this script to get out the usernames from the downloaded HTML files:
"""Extract usernames from a Youtube profile using regex"""
import re
def main():
import sys
lines = open(sys.argv[1]).read().split('\n')
#
# The html files has two <a href="..."> tags for each user: once for an
# image thumbnail, and once for a text link.
#
users = set()
for l in lines:
match = re.search('<a href="/user/(?P<name>[^"]+)" onmousedown', l)
if match:
users.add(match.group('name'))
users = list(users)
users.sort()
print users
if __name__ == '__main__':
main()
In order to access a user's subscriptions feed without the user being logged in, the user must check the "Subscribe to a channel" checkbox under his Account Sharing settings.
Currently, there is no direct way to get a channel's subscribers through the gdata API. In fact, there has been an outstanding feature request for it that has remained open for over 3 years! See Retrieving a list of a user's subscribers?.