如何解决如何从Facebook实时流或发布中刮取/获取评论?
我想从fb live或post中抓取Facebook评论,..有什么办法可以做到这一点?如果有人知道,请帮助我。
我正在使用它,但是它不起作用。
,因为它没有任何错误。但是is不会检索任何评论。
代码:
import time
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import sys
usr = ""
pwd = ""
url1 = "https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=100"
# url = sys.argv[1]
url = 'https://www.facebook.com/Typewritersvoice/photos/a.618454505026995/1619833381555764/'
# To block the notifications
options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=options,executable_path=r'C:\webdrivers\chromedriver')
# time.sleep(5)
driver.get(url1)
email = driver.find_element_by_xpath("//input[@id='email' or @name='email']")
email.send_keys(usr)
print("email entered...")
password = driver.find_element_by_xpath("//input[@id='pass']")
password.send_keys(pwd)
print("Password entered...")
button = driver.find_element_by_xpath("//button[@id='loginbutton']")
button.click()
print("button clicked...")
time.sleep(5)
print("login Successfully...")
driver.get(url)
print("Streaming URL entered...")
comments = driver.find_elements_by_css_selector('.UFICommentBody span')
print("Comment found " +str(len(comments)))
for x in range(len(comments)):
print (comments[x].text)
输出:
email entered...
Password entered...
button clicked...
login Successfully...
Streaming URL entered...
Comment found 0
,因为它没有任何错误。但是is不会检索任何评论。
解决方法
您最好使用xpath而不是css.selector,但是在新版本的facebook中,很难找到xpath,但是仍然可行。源代码中没有唯一的ID。
// div [@ aria-posinset] // div [包含(@ aria-label,'评论者')] // a / span / span [@ dir ='auto']
例如,此xpath用于注释编写者的group-post-username。不要只为新版本的Facebook忘记它
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。