He estado intentando extraer comentarios de un perfil de instagram con web scraping utilizando selenium en python, he encontrado diferentes códigos pero ninguno me ha funcionado como tal, estos mayormente utilizan bibliotecas como beautiful,requests (aunque tengo entendido que no se puede usar para instagram), etc. El código que muestro extrae comentarios con beautiful soup. También he intentado acceder a cada publicacion extrayendo el shortcode con instaloader y accediendo con selenium, tampoco me ha funcionado. Este es el código que he estado usando solo que quité mis credenciales de inicio de sesión
## Importar bibliotecas:
from unicodedata import name
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import urllib.request
import requests
import ssl
from dotenv import load_dotenv
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
import random
import time
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
## Ingresar a instagram
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")
time.sleep(10)
numa=random.randint(0,100)
username = driver.find_element(by=By.NAME, value="username")
username.click()
username.send_keys("#####")##Poner usuario
#na = random.randint(0, numa)
#driver.implicitly_wait(na)
time.sleep(5)
password = driver.find_element(by=By.NAME, value="password")##Poner contraseña
password.click()
password.send_keys('######')
#nb= random.randint(0,numa)
#Sumbit:
login = driver.find_element("css selector", "button[type='submit']").click()
#Buscar
nombre='uadeo.culiacan'
time.sleep(5)
searchbox1 = driver.find_element(
"css selector", "svg[aria-label='Búsqueda']").click()
time.sleep(3)
searchbox = driver.find_element("css selector", "input[placeholder='Busca']")
searchbox.clear()
searchbox.send_keys(nombre)
driver.find_element(By.CSS_SELECTOR, ".x1i10hfl:nth-child(1) > .x9f619 > .x9f619 > .x9f619 > .x9f619 > .x9f619 > .x9f619 > .x1lliihq > .x1lliihq").click()
## Links
time.sleep(5)
scrolldown = driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);var scrolldown=document.body.scrollHeight;return scrolldown;")
match = False
while (match == False):
last_count = scrolldown
time.sleep(3)
scrolldown = driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);var scrolldown=document.body.scrollHeight;return scrolldown;")
if last_count == scrolldown:
match = True
time.sleep(4)
img=[]
vid=[]
links = driver.find_elements("tag name", "a")
for link in links:
post = link.get_attribute('href')
if '/p/' in post:
img.append(post)
elif 'reel' in post:
vid.append(post)
if 'reels' in post:
vid.remove(post)
posts=[]
posts=vid+img
## Extraccion (aqui es el problema):
comments_instagram={} # empty dictionary
for post in posts[:]: # iterating through 10 links using for loop
driver.get(post) # fully load a moderate dynanmic webpage
res = driver.execute_script('return document.documentElement.outerHTML') # saving all source of all to variable
soup = bs(res,'html.parser') # creating a beautifulsoup object using source page variable created above
divs = soup.find_all( 'div', {'class': '_a9zs'}) # finding all div tags with a class attribute from beautifulsoup object
# iterating through all div tags and getting span tag text using for loop
comment_insta=[]
for div in divs[0:]: # iterating through all div tags using for loop
spans = div.find_all('span') # finding span tags which are within div tag with a class attribute
for span in spans: # iterating over spans variable to get requuired text
comment_insta.append(span.text) # saving cleaned comments in comments list
print(link,'\n',comment_insta)
comments_instagram.update({link:comment_insta})
time.sleep(20) # putting some sleep time between every loop