-
Notifications
You must be signed in to change notification settings - Fork 76
/
instagram.py
82 lines (71 loc) · 2.64 KB
/
instagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
try:
import argparse
import json
import requests
from fake_headers import Headers
except ModuleNotFoundError:
print("Please download dependencies from requirement.txt")
except Exception as ex:
print(ex)
'''can scrap only public instagram accounts'''
class Instagram:
@staticmethod
def build_param(username):
params = {
'username': username,
}
return params
@staticmethod
def build_headers(username):
return {
'authority': 'www.instagram.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'referer': f'https://www.instagram.com/{username}/',
'sec-ch-prefers-color-scheme': 'dark',
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Microsoft Edge";v="108"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': Headers().generate()['User-Agent'],
'x-asbd-id': '198387',
'x-csrftoken': 'VUm8uVUz0h2Y2CO1SwGgVAG3jQixNBmg',
'x-ig-app-id': '936619743392459',
'x-ig-www-claim': '0',
'x-requested-with': 'XMLHttpRequest',
}
@staticmethod
def make_request(url, params, headers, proxy=None):
response = None
if proxy:
proxy_dict = {
'http': f'http://{proxy}',
'https': f'http://{proxy}'
}
response = requests.get(
url, headers=headers, params=params, proxies=proxy_dict)
else:
response = requests.get(
url, headers=headers, params=params)
return response
@staticmethod
def scrap(username, proxy = None):
try:
headers = Instagram.build_headers(username)
params = Instagram.build_param(username)
response = Instagram.make_request('https://www.instagram.com/api/v1/users/web_profile_info/',
headers=headers, params=params, proxy=proxy)
if response.status_code == 200:
profile_data = response.json()['data']['user']
return json.dumps(profile_data)
else:
print('Error : ', response.status_code, response.text)
except Exception as ex:
print(ex)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("username", help="username to search")
parser.add_argument("--proxy", help="Proxy to use", default=None)
args = parser.parse_args()
print(Instagram.scrap(args.username, args.proxy))
# last updated on 1st January, 2023