chuyển sang mã python def getAmountOfComments(driver: WebDriver, postid: String,…
- User
chuyển sang mã python def getAmountOfComments(driver: WebDriver, post_id: String, number_comment_take: Int): Unit = { try { driver.get(s"https://mbasic.facebook.com/$post_id") var sum_links = getContentComment(driver)
while (sum_links.length < number_comment_take) { try { val next_btn = driver.findElements(By.xpath(s'//div[starts-with(@id, "see_next")]/a')) if (next_btn.size() > 0) { val sleep_time = Random.between(1, 3).toDouble // Sử dụng between để sinh số ngẫu nhiên trong khoảng từ 1 đến 3 Thread.sleep(sleep_time * 1000) next_btn.get(0).click() sum_links ++= getContentComment(driver) } else { break } } catch { case e: Exception => println(s"Lỗi khi lấy nội dung comment: ${e.getMessage}") } }} catch { case e: Exception => println(s"Lỗi nhấn xem thêm comment: ${e.getMessage}") } }
- Assistant
Here's a reworked version of your Scala code converted into Python. The function uses Selenium WebDriver to navigate to a post on "mbasic.facebook.com", fetch comments, and click on the "See next" button when available to load more comments.