-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathpinterest.py
55 lines (44 loc) · 1.68 KB
/
pinterest.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
try:
import argparse
from fake_headers import Headers
import requests
import json
except ModuleNotFoundError:
print("Please download dependencies from requirement.txt")
except Exception as ex:
print(ex)
class Pinterest:
'''This class scraps pinterest and returns a dict containing all user data'''
@staticmethod
def _generate_url(username):
return "https://pinterest.com/resource/UserResource/get/?source_url=%25{}%2F&data=%7B%22options%22%3A%7B%22field_set_key%22%3A%22profile%22%2C%22username%22%3A%22{}%22%2C%22is_mobile_fork%22%3Atrue%7D%2C%22context%22%3A%7B%7D%7D&_=1640428319046".format(username, username)
@staticmethod
def _make_request(url):
headers = Headers().generate()
response = requests.get(url, headers=headers)
return response
@staticmethod
def scrap(username):
try:
try:
url = Pinterest._generate_url(username)
response = Pinterest._make_request(url)
if response.status_code == 200:
response = response.json()
else:
print("Failed to get Data!")
exit()
except Exception as ex:
print("Error", ex)
exit()
json_data = response
data = json_data['resource_response']['data']
return json.dumps(data)
except Exception as ex:
print(ex)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("username", help="username to search")
args = parser.parse_args()
print(Pinterest.scrap(args.username))
# last updated - 8th July,2022