Facebook Selenium如何获得喜欢的人数 [英] Facebook Selenium How can I get the number of likes

查看:34
本文介绍了Facebook Selenium如何获得喜欢的人数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码获取喜欢该帖子的人数.我尝试了两种方法都不起作用.它确实返回错误代码.

有人可以帮忙,对此表示感谢.

源代码:

  def get_likes(self):返回self.driver.find_element_by_xpath('//span [@ data-store ="{{" reactionType:1}"]]/span/span').get_attribute("innerHTML")def get_likes():return self.driver.find_element_by_xpath('//span [@ data-store ="{\" reactionType \:1}"]/span/span').get_attribute(" innerHTML) 

语法错误:无法在文档"上执行评估":字符串'//span [@ data-store ="{" reactionType:1}"]]/span/span不是有效的XPath表达式.

解决方案

也尝试以不同的方式进行转义,但结果相同-似乎xpath与html和"中的问题有关表示它们已关闭,所以

driver.find_element_by_xpath('//span [@ id ='u_0_r']]'.get_attribute('data-store')

仅检测开头 {

div

Id会自动生成,并且图标的位置将根据点击次数进行排序.

您可以使用 i 标签的 class ,直到有更好的解决方案为止,这真的会让我感兴趣:

从硒导入Webdriver的

 驱动程序= webdriver.Chrome(executable_path = r'C:\ Program Files \ ChromeDriver \ chromedriver.exe')html_content ='''< div class ="scrollAreaColumn">< span name =一个"数据存储="{"reactionType":1}"< span> 133< span></span>< span name =两个"数据存储="{"reactionType":1}"< i class =" _2ep2 img sp_FnpQQMXWL5W sx_4760b1"</i>< span> 128</span></span>< span name =三个"数据存储="{"reactionType":1}"< i class =" _2ep2 img sp_FnpQQMXWL5W sx_4760b1 xyz"</i>< span> 128</span></span></div>'''driver.get(" data:text/html; charset = utf-8,{html_content}" .format(html_content = html_content))count = driver.find_element_by_xpath('//i [@class ="_ 2ep2 img sp_FnpQQMXWL5W sx_4760b1"]/following-sibling :: span').text打印(数)driver.close() 

I am trying to get the numbers of people who liked the post with the below code. I tried both methods both are not working. It does return error code.

Could someone assist, appreciate that.

Source Code : https://m.facebook.com/ufi/reaction/profile/browser/?ft_ent_identifier=3647203655325514

def get_likes(self):
    return self.driver.find_element_by_xpath('//span[@data-store="{"reactionType":1}"]/span/span').get_attribute("innerHTML")

def get_likes(self):
return self.driver.find_element_by_xpath('//span[@data-store="{\"reactionType\":1}"]/span/span').get_attribute("innerHTML")

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[@data-store="{"reactionType":1}"]/span/span' is not a valid XPath expression.

解决方案

Also tried to escape in different ways, but same result - Seems that xpath has a problem with the " from the html and means that they were closed, so

driver.find_element_by_xpath('//span[@id="u_0_r"]').get_attribute('data-store')

only detects the opening {

Id of parent div is generated automatically and position of icons in it is ordered by count of clicks.

You could use the class of the i tag until there is a better solution, that really would interest me:

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')

html_content = '''
<div class="scrollAreaColumn">
        <span name="one" data-store="{"reactionType":1}">
         <span>133</span>
        </span>
        <span name="two" data-store="{"reactionType":1}">
         <i class="_2ep2 img sp_FnpQQMXWL5W sx_4760b1"></i>
         <span>128</span>
        </span>
        <span name="three" data-store="{"reactionType":1}">
         <i class="_2ep2 img sp_FnpQQMXWL5W sx_4760b1 xyz"></i>
         <span>128</span>
        </span>
</div>       
'''
driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
count = driver.find_element_by_xpath('//i[@class="_2ep2 img sp_FnpQQMXWL5W sx_4760b1"]/following-sibling::span').text
print(count)

driver.close()

这篇关于Facebook Selenium如何获得喜欢的人数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆