-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create instagram_crawler.py #2509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
28671c0
Create instagram_crawler.py
yogeshwaran01 7c6520c
codespell --ignore-words-list=followings
cclauss 3cd53cb
Update web_programming/instagram_crawler.py
yogeshwaran01 cd6f44e
Update web_programming/instagram_crawler.py
yogeshwaran01 52ec0f6
Update web_programming/instagram_crawler.py
yogeshwaran01 66c157d
Update web_programming/instagram_crawler.py
yogeshwaran01 c50204c
Update web_programming/instagram_crawler.py
yogeshwaran01 fcdf0eb
Update web_programming/instagram_crawler.py
yogeshwaran01 6756207
Update web_programming/instagram_crawler.py
yogeshwaran01 3401506
Update instagram_crawler.py
yogeshwaran01 3e92923
Add doctests
cclauss b820a36
fixup! except (json.decoder.JSONDecodeError, KeyError):
cclauss 5169ce8
if getenv("CONTINUOUS_INTEGRATION"): return
cclauss 81ac0b1
Update instagram_crawler.py
cclauss 2d7c21a
Update web_programming/instagram_crawler.py
yogeshwaran01 fd09b9a
added fake_useragent
yogeshwaran01 5343049
Update instagram_crawler.py
yogeshwaran01 f7254c1
Comment out doctests
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#!/usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
yogeshwaran01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
import json | ||
|
||
headers = { | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) " | ||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" | ||
} | ||
|
||
# Usage | ||
""" | ||
>>> user = Instagram("github") | ||
>>> user.is_verified | ||
True | ||
>>> user.get_biography | ||
Built for developers. | ||
|
||
""" | ||
|
||
|
||
class InstagramUser: | ||
""" | ||
Class Instagram crawl instagram user information | ||
""" | ||
|
||
def __init__(self, username): | ||
self.username = username | ||
self.url = f"https://www.instagram.com/{username}/" | ||
|
||
def get_json(self): | ||
yogeshwaran01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return json of user information | ||
""" | ||
|
||
html = requests.get(self.url, headers=headers) | ||
soup = BeautifulSoup(html.text, "html.parser") | ||
try: | ||
return html_1(soup) | ||
except json.decoder.JSONDecodeError: | ||
return html_2(soup) | ||
|
||
@property | ||
def number_of_followers(self) -> int: | ||
""" | ||
return number of followers | ||
""" | ||
|
||
info = self.get_json() | ||
return info["edge_followed_by"]["count"] | ||
|
||
@property | ||
def no_of_followings(self) -> int: | ||
""" | ||
return number of followings | ||
""" | ||
|
||
info = self.get_json() | ||
return info["edge_follow"]["count"] | ||
|
||
@property | ||
def no_of_posts(self) -> int: | ||
""" | ||
return number of posts | ||
""" | ||
|
||
info = self.get_json() | ||
return info["edge_owner_to_timeline_media"]["count"] | ||
|
||
@property | ||
def get_biography(self) -> str: | ||
yogeshwaran01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return biography of user | ||
""" | ||
|
||
info = self.get_json() | ||
return info["biography"] | ||
|
||
@property | ||
def fullname(self) -> str: | ||
""" | ||
return fullname of the user | ||
""" | ||
|
||
info = self.get_json() | ||
return info["full_name"] | ||
|
||
@property | ||
def get_username(self) -> str: | ||
""" | ||
return the username of the user | ||
""" | ||
|
||
info = self.get_json() | ||
return info["username"] | ||
|
||
@property | ||
def get_profile_pic(self) -> str: | ||
yogeshwaran01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return the link of profile picture | ||
""" | ||
|
||
info = self.get_json() | ||
return info["profile_pic_url_hd"] | ||
|
||
@property | ||
def get_website(self) -> str: | ||
""" | ||
return the users's website link | ||
""" | ||
|
||
info = self.get_json() | ||
return info["external_url"] | ||
|
||
@property | ||
def get_email(self) -> str: | ||
""" | ||
return the email id of user if | ||
available | ||
""" | ||
|
||
info = self.get_json() | ||
return info["business_email"] | ||
|
||
@property | ||
def is_verified(self) -> bool: | ||
""" | ||
check the user is verified | ||
""" | ||
|
||
info = self.get_json() | ||
return info["is_verified"] | ||
|
||
@property | ||
def is_private(self) -> bool: | ||
""" | ||
check user is private | ||
""" | ||
|
||
info = self.get_json() | ||
return info["is_private"] | ||
|
||
|
||
def html_1(soup): | ||
scripts = soup.find_all("script") | ||
main_scripts = scripts[4] | ||
data = main_scripts.contents[0] | ||
info_object = data[data.find('{"config"') : -1] | ||
info = json.loads(info_object) | ||
info = info["entry_data"]["ProfilePage"][0]["graphql"]["user"] | ||
return info | ||
|
||
|
||
def html_2(soup): | ||
scripts = soup.find_all("script") | ||
main_scripts = scripts[3] | ||
data = main_scripts.contents[0] | ||
info_object = data[data.find('{"config"') : -1] | ||
info = json.loads(info_object) | ||
info = info["entry_data"]["ProfilePage"][0]["graphql"]["user"] | ||
return info | ||
|
||
|
||
if __name__ == "__main__": | ||
user = InstagramUser("github") | ||
print(f"{user.is_verified = }") | ||
print(f"{user.get_biography = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.