作者kiwigo1000 (快變小黑人了.....)
看板Python
標題[問題] 抓取臉書貼文的所有留言與回覆
時間Wed Jul 28 16:44:40 2021
各位前輩好
目前在練習抓取這則臉書貼文的所有留言,以及所有留言內的回覆內容。
https://www.facebook.com/JIEJIE.UNCLECAT/posts/369845707834873
目前有辦法點擊抓出所有的留言,但是不太清楚該如何再點擊抓出留言內的回覆內容。
如果想要再抓出回覆內容的話,是在要while迴圈當中放入點擊「n則回覆」的指令嗎?
如果是的話,我該如何放進去呢?
有請各位前輩給個方向,謝謝。
-------
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
import requests
from bs4 import BeautifulSoup
from selenium.common.exceptions import NoSuchElementException
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("
https://www.facebook.com")
username = WebDriverWait(driver,
10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"input[name='email']")))
password = WebDriverWait(driver,
10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='pass']")))
username.clear()
username.send_keys("my email")
password.clear()
password.send_keys("my password")
button = WebDriverWait(driver,
2).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"button[type='submit']"))).click()
time.sleep(3)
driver.get("
https://www.facebook.com/JIEJIE.UNCLECAT/posts/369845707834873")
time.sleep(5)
while(True):
try:
driver.find_element_by_xpath("//*[contains(text(), '查看更多留言
')]").click()
driver.execute_script("window.scrollTo(0,
document.body.scrollHeight);")
time.sleep(5)
except NoSuchElementException:
break
soup = BeautifulSoup(driver.page_source, 'html.parser')
comments = soup.find_all("div", class_="kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x
c1et5uql")
posts=[]
for comment in comments:
posts.append(comment.text)
posts
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 119.171.145.128 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1627461882.A.7A1.html
推 TitanEric: 感覺可以接API?不過不曉得現在fb機制是如何 07/28 20:47
推 goitaly: API好麻煩 尤其是group要先審查 07/29 16:57
→ goitaly: 有個class是固定的用for loop去跑到底就好 07/29 16:58