简体   繁体   English

无法从Facebook获取带有硒的所有名称-Python 3

[英]Can't get all names with selenium from Facebook - Python 3

i'm creating a scraper using python and selenium to improve my ability with python. 我正在使用python和selenium创建一个刮板,以提高我使用python的能力。 I'm having trouble with selecting certain elements. 我在选择某些元素时遇到了麻烦。

On facebook i'm trying to scrape the list of someone friends. 在Facebook上,我正在尝试抓取某人朋友的列表。 This is the piece of code i wrote 这是我写的代码

nomifb = driver.find_elements_by_xpath("//div[@class='fsl fwb fcb']")
print("We've found " + str(len(nomifb)) + " friends!")"
file = open(filename, "w")
for i in range(len(nomifb)):
    nomifb_txt.append(nomifb[i].text)
nomifb_txt.sort()
for i in range(len(nomifb)):
    file.write(nomifb_txt[i] + "\n")
file.close()

I get the div the contain the names using the "fsl fwb fcb" classes. 我使用“ fsl fwb fcb”类使div包含名称。 That seems to be working on people with not a lot of friends. 这似乎正在与没有很多朋友的人一起工作。 If i get over 400 friends it seems to miss about 5% of it, and i cant seem to figure out why :/ 如果我有超过400个朋友,似乎会错过大约5%,而我似乎无法弄清楚为什么:/

Is there anyway to improve the find_elements so that i get every person name? 无论如何,有没有改进find_elements以便我得到每个人的名字?

I checked with chrome to see if there was any problem with my script but it seems that the "fsl fwb fcb" tag is used less time than the total number of friends and there lies the problem 我检查了chrome,看我的脚本是否有问题,但似乎“ fsl fwb fcb”标签的使用时间少于朋友的总数,这就是问题所在

Also i seems to have a too-hacky solution for the scrolldown, it goes down until it find 另外我似乎对向下滚动有一个过于棘手的解决方案,它会一直下降直到找到

element = driver.find_element_by_xpath("//*[contains(text(), 'Altre informazioni su')]")

Which is "Other information about" but as you can see it lack the support for other languages. 这是“关于其他信息”,但是如您所见,它缺乏对其他语言的支持。 How would i go about selecting it in a better way? 我将如何更好地选择它?

Sorry for the newbie question, hope you can spare a moment to teach me better :) 对新手问题很抱歉,希望您能抽出一点时间教我更好的方法:)

Cheers 干杯

EDIT I've seemed to fix it! 编辑我似乎已经解决了! The problem was that facebook count in the friends number also deleted account, and that's where the discrepancy come from. 问题是,Facebook的朋友数中的计数也被删除了,这就是差异的来源。

您可以尝试通过元素名称element = driver.find_element_by_name("name of tag")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM