Skip to content

Commit 04c1237

Browse files
authored
Merge pull request Python-World#319 from swaroopmaddu/master
Instagram Profile data Grabber
2 parents 962f296 + ea9fe9b commit 04c1237

5 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎projects/Instagram_profile/Readme.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ pip install -r requirements.txt
88
### Execute Program
99
```
1010
python InstgramProfile.py <username>
11-
```
11+
```
12+
13+
### Sample run
14+
15+
16+
<img src="output.png"/>

‎projects/Instagram_profile/main.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import requests
2+
from lxml import html
3+
import re
4+
import sys
5+
import pprint
6+
from profilepic import pp_download
7+
8+
def banner():
9+
print('\t""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""')
10+
print('\t InstgramProfile data graber ')
11+
print('\t""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""')
12+
13+
14+
def main(username):
15+
banner()
16+
'''main function accept instagram username
17+
return an dictionary object containging profile deatils
18+
'''
19+
20+
url = "https://www.instagram.com/{}/?hl=en".format(username)
21+
page = requests.get(url)
22+
tree = html.fromstring(page.content)
23+
data = tree.xpath('//meta[starts-with(@name,"description")]/@content')
24+
25+
if data:
26+
data = tree.xpath('//meta[starts-with(@name,"description")]/@content')
27+
data = data[0].split(', ')
28+
followers = data[0][:-9].strip()
29+
following = data[1][:-9].strip()
30+
posts = re.findall(r'\d+[,]*', data[2])[0]
31+
name = re.findall(r'name":"([^"]+)"', page.text)[0]
32+
aboutinfo = re.findall(r'"description":"([^"]+)"', page.text)[0]
33+
instagram_profile = {
34+
'success': True,
35+
'profile': {
36+
'name': name,
37+
'profileurl': url,
38+
'username': username,
39+
'followers': followers,
40+
'following': following,
41+
'posts': posts,
42+
'aboutinfo': aboutinfo
43+
}
44+
}
45+
else:
46+
instagram_profile = {
47+
'success': False,
48+
'profile': {}
49+
}
50+
return instagram_profile
51+
52+
53+
# python main.py username
54+
if __name__ == "__main__":
55+
56+
if len(sys.argv) == 2:
57+
output = main(sys.argv[-1])
58+
pp_download(sys.argv[-1])
59+
pprint.pprint(output)
60+
61+
else:
62+
print('Invalid paramaters Valid Command \n\tUsage : python main.py username')
300 KB
Loading
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from datetime import datetime
2+
from tqdm import tqdm
3+
import requests
4+
import re
5+
import sys
6+
from PIL import Image
7+
8+
9+
#Function to download profile picture of instagram accounts
10+
def pp_download(username):
11+
12+
url = "https://www.instagram.com/{}/".format(username)
13+
x = re.match(r'^(https:)[/][/]www.([^/]+[.])*instagram.com', url)
14+
15+
if x:
16+
check_url1 = re.match(r'^(https:)[/][/]www.([^/]+[.])*instagram.com[/].*\?hl=[a-z-]{2,5}', url)
17+
check_url2 = re.match(r'^(https:)[/][/]www.([^/]+[.])*instagram.com$|^(https:)[/][/]www.([^/]+[.])*instagram.com/$', url)
18+
check_url3 = re.match(r'^(https:)[/][/]www.([^/]+[.])*instagram.com[/][a-zA-Z0-9_]{1,}$', url)
19+
check_url4 = re.match(r'^(https:)[/][/]www.([^/]+[.])*instagram.com[/][a-zA-Z0-9_]{1,}[/]$', url)
20+
21+
if check_url3:
22+
final_url = url + '/?__a=1'
23+
24+
if check_url4:
25+
final_url = url + '?__a=1'
26+
27+
if check_url2:
28+
final_url = print("Please enter an URL related to a profile")
29+
exit()
30+
31+
if check_url1:
32+
alpha = check_url1.group()
33+
final_url = re.sub('\\?hl=[a-z-]{2,5}', '?__a=1', alpha)
34+
35+
try:
36+
if check_url3 or check_url4 or check_url2 or check_url1:
37+
req = requests.get(final_url)
38+
get_status = requests.get(final_url).status_code
39+
get_content = req.content.decode('utf-8')
40+
41+
if get_status == 200:
42+
print("\nDownloading the image...")
43+
find_pp = re.search(r'profile_pic_url_hd\":\"([^\'\" >]+)', get_content)
44+
pp_link = find_pp.group()
45+
pp_final = re.sub('profile_pic_url_hd":"', '', pp_link)
46+
file_size_request = requests.get(pp_final, stream=True)
47+
file_size = int(file_size_request.headers['Content-Length'])
48+
block_size = 1024
49+
t=tqdm(total=file_size, unit='B', unit_scale=True, desc=username, ascii=True)
50+
with open(username + '.jpg', 'wb') as f:
51+
for data in file_size_request.iter_content(block_size):
52+
t.update(len(data))
53+
f.write(data)
54+
t.close()
55+
#Show image
56+
im = Image.open(username +".jpg")
57+
im.show()
58+
print("Profile picture downloaded successfully")
59+
60+
except Exception:
61+
print('error')
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1-
lxml==4.5.1
1+
appdirs==1.4.3
2+
beautifulsoup4==4.9.2
3+
bs4==0.0.1
4+
CacheControl==0.12.6
5+
certifi==2019.11.28
6+
chardet==3.0.4
7+
colorama==0.4.3
8+
contextlib2==0.6.0
9+
distlib==0.3.0
10+
distro==1.4.0
11+
html5lib==1.0.1
12+
idna==2.8
13+
ipaddr==2.2.0
14+
lockfile==0.12.2
15+
lxml==4.5.2
16+
msgpack==0.6.2
17+
packaging==20.3
18+
pep517==0.8.2
19+
Pillow==7.2.0
20+
progress==1.5
21+
pyparsing==2.4.6
22+
pytoml==0.1.21
23+
requests==2.22.0
24+
retrying==1.3.3
25+
six==1.14.0
26+
soupsieve==2.0.1
27+
tqdm==4.50.0
28+
urllib3==1.25.8
29+
webencodings==0.5.1

0 commit comments

Comments
 (0)