def kotlin_hashtag(): kotlin_hashtag_name = "kotlin" kotlin_hashtag_url = f"https://www.instagram.com/tags/{kotlin_hashtag_name}/" kotlin_hashtag = Hashtag(url=kotlin_hashtag_url, name=kotlin_hashtag_name) kotlin_hashtag.static_load() return kotlin_hashtag
def _load_hashtag_data(tags: list): """Scrape hashtag data for the given hashtags""" hashtag_objs = [] with tqdm.tqdm(total=len(tags), position=0) as progress: for tag in tags: time.sleep(abs(np.random.normal(3, 2))) hashtag = Hashtag.from_hashtag(tag) hashtag.static_load() hashtag_objs.append({ "hashtag": tag, "posts": hashtag.amount_of_posts, "datetime": datetime.datetime.now() }) progress.update(1) return hashtag_objs
import numpy as np import matplotlib.pyplot as plt import pandas as pd sys.path.insert(0, os.path.abspath("..")) from instascrape import Hashtag # We will start by running a loop and scraping data at random intervals until the predefined timeframe has elapsed. # In[2]: # Metadata TOTAL_TIME = 60 # Total time WAIT_TIME = 5 # Mean wait time for random normal distribution photography = Hashtag.from_hashtag("photography") instagram = Hashtag.from_hashtag("instagram") # Create a list of tuples containing data scraped at random # intervals during the time period current_time = datetime.datetime.now() end_time = current_time + datetime.timedelta(seconds=TOTAL_TIME) photography_data = [] instagram_data = [] while current_time < end_time: # Wait for normally randomized amount of time rand_time = abs(np.random.normal(WAIT_TIME, 1.5)) time.sleep(WAIT_TIME) # Scrape data and append to respective lists photography.static_load()
def test_from_hashtag(kotlin_hashtag): result_hashtag: Hashtag = Hashtag.from_hashtag(hashtag=kotlin_hashtag.name) result_hashtag.static_load() assert result_hashtag.url == kotlin_hashtag.url assert result_hashtag.name == kotlin_hashtag.name
def page_instance(self, url, headers): random_hashtag = Hashtag(url) random_hashtag.scrape(headers=headers) return random_hashtag
def test_from_hashtag(kotlin_hashtag): result_hashtag: Hashtag = Hashtag.from_hashtag(hashtag=kotlin_hashtag.name) assert result_hashtag.url == kotlin_hashtag.url