I'm just using a code I found on this website https://www.bestproxyreviews.com/instagram-scraper/. The code is this:
from selenium import webdriver
class InstagramScraper:
def __init__(self, post_url):
self.post_url = post_url
self.comments = []
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
self.chrome = webdriver.Chrome(chrome_options=chrome_options)
def scrape_comments(self):
browser = self.chrome.get(self.post_url)
content = self.chrome.page_source
comments = self.chrome.find_element_by_class_name("XQXOT").find_elements_by_class_name("Mr508")
for comment in comments:
d = comment.find_element_by_class_name("ZyFrc").find_element_by_tag_name("li").find_element_by_class_name("P9YgZ").find_element_by_tag_name("div")
d = d.find_element_by_class_name("C4VMK")
poster = d.find_element_by_tag_name("h3").text
post = d.find_element_by_tag_name("span").text
self.comments.append({
"poster": poster,
"post": post
})
return self.comments
post_url = "https://www.instagram.com/p/CTu1euSp6jZ/"
x = InstagramScraper(post_url)
x.scrape_comments()
And the error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".XQXOT"}
(Session info: headless chrome=94.0.XXXX.XX)
What I'm trying to do is to get all the comments of a post on .csv.
Any hint?