This code first creates an instance of the Instaloader class and logs in to Instagram (if you have an account). It then retrieves a post by its URL, extracts its caption, comments, number of likes, and hashtags, and prints them out to the console. Note that you'll need to replace "your_account" with your Instagram account name in the load_session_from_file method if you want to login. Additionally, this code assumes that the post URL is stored in the post_url variable, so make sure to replace that with the actual post URL you want to scrape.
import instaloader
import re
# Create an instance of the Instaloader class
loader = instaloader.Instaloader()
# Login to Instagram (optional)
loader.context.log("Login...")
loader.load_session_from_file("your_account")
# Retrieve a post by its URL
post_url = "https://www.instagram.com/p/ABC123/"
post = instaloader.Post.from_shortcode(loader.context, post_url.split("/")[-2])
# Extract the post's caption
caption = post.caption
print("Caption: ", caption)
# Extract the post's comments
comments = []
for comment in post.get_comments():
comments.append(comment.text)
print("Comments: ", comments)
# Extract the post's number of likes
num_likes = post.likes
print("Likes: ", num_likes)
# Extract the post's hashtags
hashtags = re.findall(r"#(\w+)", caption)
print("Hashtags: ", hashtags)