This is second post in series of web scraping FB for creation of own API. Today I will show how to accept friend request. Previously in entry Your own Facebook API - logging in I showed how to log into your Facebook account.
Getting list of friend request is quite simple using low-end Facebook interface. You just need to go to https://mbasic.facebook.com/friends/center/requests/ and parse every “a” tag which “href” attribute starts with “/a/notifications.php?confirm=”. If you care about name of user who sent you friend request, you’d like to remember last parsed “a” tag with “href” attribute starting with “/friends/hovercard/mbasic/” and read text value from it. Of course, if your account has many friend requsts, not all of them will be available under single page. To get more of them you’d need to find and parse “a” tag which “href” attribute starts with “/friends/center/requests/". But it is out of this post’s scope.
We need to add class field containing requests url and method which will return friend requests. Whole class should look following
|
|
As you can see, I created new classes: FriendRequest and FriendConfirmParser. First is an object representing friend request, containing user name and approval link. The second one is another parser, to find information we’re looking for. They should look like that
|
|
Having above code we can easily get couple first friend requests, display usernames or accept their to our friend list, like on example below.
|
|