RocketAPI for Instagram

FREEMIUM
Verified
āœ“
By RocketAPI | Updated 9 days ago | Social
Popularity

9.9 / 10

Latency

2,279ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (2)

Scraping User Followers and Saving to .CSV using Python and PHP

This tutorial will demonstrate how to scrape all user followers from Instagram and save the data as a .CSV file using both Python and PHP. We will use the requests library for Python and the cURL library for PHP. We will also use the pandas library for Python and the fputcsv() function for PHP. By the end of this tutorial, you will be able to scrape user followers and save it to a .CSV file.

Python

This tutorial will demonstrate how to scrape all user followers and save it as a .CSV file using Python.

First, we need to install the necessary libraries.

pip install requests
pip install pandas

Once the libraries are installed, we can start writing the code.

First, we need to set up the request. We will use the requests library to do this. Weā€™ll need to provide a URL, headers, and data.

The URL is:

https://rocketapi-for-instagram.p.rapidapi.com/instagram/user/get_followers

The headers are:

Content-Type: application/json
X-RapidAPI-Key: <your-api-key>
X-RapidAPI-Host: rocketapi-for-instagram.p.rapidapi.com

And the data is:

{
    "id": 12281817,
    "count": 100,
    "max_id": null
}

We can use the following code to set up the request:

url = "https://rocketapi-for-instagram.p.rapidapi.com/instagram/user/get_followers"
headers = {
    "Content-Type": "application/json",
    "X-RapidAPI-Key": "<your-api-key>",
    "X-RapidAPI-Host": "rocketapi-for-instagram.p.rapidapi.com"
}
data = {
    "id": 12281817,
    "count": 100,
    "max_id": null
}

Next, we need to make the request and get the response. To do this, weā€™ll use the following code:

response = requests.post(url, headers=headers, data=json.dumps(data))

Once we have the response, we can start scraping. We will use the JSON item example of [ā€˜responseā€™][ā€˜bodyā€™][ā€˜usersā€™] list that was provided earlier. We will create a dictionary and add the data to it.

We can use a for loop to iterate through the response and add the data to the dictionary. The code for this is:

data_dict = {}

for item in response['response']['body']['users']:
    data_dict[item['pk']] = {
        'username': item['username'],
        'full_name': item['full_name'],
        'is_private': item['is_private'],
        'pk_id': item['pk_id'],
        'profile_pic_url': item['profile_pic_url'],
        'profile_pic_id': item['profile_pic_id'],
        'is_verified': item['is_verified'],
        'has_anonymous_profile_picture': item['has_anonymous_profile_picture'],
        'has_highlight_reels': item['has_highlight_reels'],
        'account_badges': item['account_badges'],
        'latest_reel_media': item['latest_reel_media']
    }

Once we have the data in the dictionary, we can save it to a .CSV file. To do this, weā€™ll need to use the pandas library. We can use the following code to save the data to a .CSV file:

import pandas as pd

# convert the dictionary to a dataframe
data_df = pd.DataFrame.from_dict(data_dict, orient='index')

# save the dataframe to a csv file
data_df.to_csv('user_followers.csv', index=False)

This code will save the data from the dictionary to a .CSV file called user_followers.csv.

PHP

This tutorial will demonstrate how to scrape all user followers and save it as a .CSV file using PHP.

First, we need to set up the request. We will use the cURL library to do this. Weā€™ll need to provide a URL, headers, and data.

The URL is:

https://rocketapi-for-instagram.p.rapidapi.com/instagram/user/get_followers

The headers are:

Content-Type: application/json
X-RapidAPI-Key: <your-api-key>
X-RapidAPI-Host: rocketapi-for-instagram.p.rapidapi.com

And the data is:

{
    "id": 12281817,
    "count": 100,
    "max_id": null
}

We can use the following code to set up the request:

$url = "https://rocketapi-for-instagram.p.rapidapi.com/instagram/user/get_followers";
 
$headers = array(
    "Content-Type: application/json",
    "X-RapidAPI-Key: <your-api-key>",
    "X-RapidAPI-Host: rocketapi-for-instagram.p.rapidapi.com"
);
 
$data = array(
    "id" => 12281817,
    "count" => 100,
    "max_id" => null
);
 
$data_string = json_encode($data);
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

Next, we need to make the request and get the response. To do this, weā€™ll use the following code:

$response = curl_exec($curl);

Once we have the response, we can start scraping. We will use the JSON item example of [ā€˜responseā€™][ā€˜bodyā€™][ā€˜usersā€™] list that was provided earlier. We will create an array and add the data to it.

We can use a foreach loop to iterate through the response and add the data to the array. The code for this is:

$data_array = array();
 
foreach($response['response']['body']['users'] as $item){
    $data_array[] = array(
        'pk' => $item['pk'],
        'username' => $item['username'],
        'full_name' => $item['full_name'],
        'is_private' => $item['is_private'],
        'pk_id' => $item['pk_id'],
        'profile_pic_url' => $item['profile_pic_url'],
        'profile_pic_id' => $item['profile_pic_id'],
        'is_verified' => $item['is_verified'],
        'has_anonymous_profile_picture' => $item['has_anonymous_profile_picture'],
        'has_highlight_reels' => $item['has_highlight_reels'],
        'account_badges' => $item['account_badges'],
        'latest_reel_media' => $item['latest_reel_media']
    );
}

Once we have the data in the array, we can save it to a .CSV file. To do this, weā€™ll need to use the fputcsv() function. We can use the following code to save the data to a .CSV file:

$fp = fopen('user_followers.csv', 'w');
 
foreach ($data_array as $fields) {
    fputcsv($fp, $fields);
}
 
fclose($fp);

This code will save the data from the array to a .CSV file called user_followers.csv.

With RocketAPI, you can easily scrape all user followers and save it as a .CSV file using either Python or PHP. Our easy-to-use API will help you quickly get the data you need. If you have any questions or need a custom solution, please feel free to contact us on Telegram at https://t.me/rocketapi, by email at contact@rocketapi.io, or on our website https://rocketapi.io. We look forward to hearing from you!