Loading Ajax with Python Requests


For a personal project, I'm trying to get a full friends list of a user (myself for now) from Facebook using Requests and BeautifulSoup. The main friends page however displays only 20, and the rest are loaded with Ajax when you scroll down.

The request url looks something like this (method is GET):


My question is, is it possible to recreate the dynamically generated tokens such as the __dyn, cursor, collection_token etc. to send manually in my request? Is there some way to figure out how they are generated or is it a lost cause?

I know that the current Facebook API does not support viewing a full friends list. I also know that I can do this with Selenium, or some other browser simulator, but that feels way too slow, ideally I want to scrape thousands of friends lists (of users whose friends lists are public) in a reasonable time.

My current code is this:

import requests
from bs4 import BeautifulSoup
with requests.Session() as S:
    requests.utils.add_dict_to_cookiejar(S.cookies, {'locale': 'en_US'})
    form = {}
    form['email'] = 'myusername'
    form['pass'] = 'mypassword'
    response = S.post('https://www.facebook.com/login.php?login_attempt=1&lwv=110', data=form)
    # Im logged in
    page = S.get('https://www.facebook.com/yoshidakai/friends?source_ref=pb_friends_tl')

Any help will be appreciated, including other methods to achieve this :)

Show source
| facebook   | python   | python-requests   | web-scraping   2016-10-12 18:10 0 Answers

Answers to Loading Ajax with Python Requests ( 0 )

Leave a reply to - Loading Ajax with Python Requests

◀ Go back