Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
5
Indexable
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
import random

def scrape_comments(post_url):
    service = Service(executable_path="comments/chromedriver.exe")
    driver = webdriver.Chrome(service=service)
    driver.get(post_url)
    time.sleep(2)

    cookies_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Permitir todos os cookies')]")
    while cookies_button:
        try:
            cookies_button.click()
        except Exception as e:
            break

    more_comments_button = driver.find_element(By.CLASS_NAME, "x1lliihq")
    while more_comments_button:
        try:
            more_comments_button.click()
            time.sleep(2)
            more_comments_button = driver.find_element(By.CLASS_NAME, "x1lliihq")
        except Exception as e:
            break

    comment_elements = driver.find_element(By.CLASS_NAME, "_a9ym")

    comments_with_usernames = [(comment.find_element(By.XPATH, ".//h3").text, comment.find_element(By.XPATH, ".//span").text) for comment in comment_elements]

    driver.quit()

    return comments_with_usernames
Editor is loading...
Leave a Comment