This tutorial is built on part 1.

Recap

In part 1, we wrote code for retrieving tweets associated with user provided hashtag and then scrape them for other tweets. In part 2, we will use beautiful soup to scrape Instagram posts from hashtags.

Scraping Instagram

The good thing about this code is that you do not need to log into any Instagram account. Anyone can access publicly available posts on Instagram using the hashtag. For example if you want to see the posts for the hashtag #newyork, you can do so by using the following URL:

https://www.instagram.com/explore/tags/newyork

We use the above link to retrieve the posts, the HTML response using beautiful soup and retrieve the JSON dictionary. After that, we extract all the text associated with the posts, extract hashtags and combine them with our twitter hashtags list before returning it to the user.

We start by importing the required libraries.

import re
import bs4
import requests
import json

If you recall, we got user input and cast it to the variable “tag” and then using out “clean_tag” function, clean it and cast it to “search_word” variable. For revision, the code is as follow:

tag =str(input(“Please enter your hashtag/text: “))

Find Relevant Top Hashtags using Python - Part 1

#python #instagram #hashtag #web scraping

Find Relevant Top Hashtags using Python - Part 2
26.85 GEEK