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
import pickle | |
with open('pickle_file.pkl', 'rb') as file: | |
data = pickle.load(file) | |
print(data) | |
num_list = [range(1000000)] | |
# How to save a Python object to a pickle file | |
with open('new_pickle.pkl', 'wb') as file: |
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
import requests | |
url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" | |
def download(url): | |
# Send a request to the given url | |
response = requests.get(url, allow_redirects=True) | |
# Extract the file extension | |
extension = url.split('.')[-1] | |
# Save the file locally |
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
# Import the module | |
import instaloader | |
# Create an instance of Instaloader class | |
bot = instaloader.Instaloader() | |
# Load a profile from an Instagram handle | |
profile = instaloader.Profile.from_username(bot.context, 'python_scripts') | |
print(type(profile)) |
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
# Login with username and password in the script | |
bot.login(USERNAME, PASSWORD) | |
# Interactive login on terminal | |
bot.interactive_login(USER) # Asks for password in the terminal |
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
# Retrieve the usernames of all followers | |
followers = [follower.username for follower in profile.get_followers()] | |
# Retrieve the usernames of all followees | |
followees = [followee.username for followee in profile.get_followees()] |
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
# Load a new profile | |
profile = instaloader.Profile.from_username(bot.context, 'towardsdatascience') | |
# Get all posts in a generator object | |
posts = profile.get_posts() | |
# Iterate and download | |
for index, post in enumerate(posts, 1): | |
bot.download_post(post, target=f"{profile.username}_{index}") |
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
# Load the hashtag | |
hashtag = instaloader.Hashtag.from_name(bot.context, 'python') | |
# Load posts with `python` tag into a generator object | |
python_posts = hashtag.get_posts() | |
# Iterate over posts to download them | |
for index, post in enumarate(python_posts, 1): | |
bot.download_post(post, target=f'{hashtag.name}_{index}') |
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
def tops_posts_from_hashtag(hashtag_name: str, max_count: int): | |
""" | |
A function that downloads top {max_count} posts from a hashtag | |
""" | |
# Load the hashtag object into a variable | |
hashtag = instaloader.Hashtag.from_name(bot.context, hashtag_name) | |
# Get top posts in a generator | |
posts = hashtag.get_top_posts() | |
for index in range(1, max_count + 1): | |
try: |
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
import psycopg2 | |
try: | |
# Establish a connection | |
connection = psycopg2.connect(user='nufavqtimheyhv', | |
password="33913ed88ebcb400c1b353a07b9f11e8480c4b4ea437820a2c7dc04e09b98e7c", | |
port=5432, host='ec2-3-208-50-226.compute-1.amazonaws.com', | |
database='da1fvg7qnm795d') | |
# Create a cursor object | |
cursor = connection.cursor() |
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
import mysql.connector | |
# Store the arguments in a dictionary | |
config = { | |
'user': 'bex', | |
'password': 'my_password', | |
'database': 'db_name', | |
'host': 'host_url' | |
} |