分析过去一年Facebook群组的数量和成员水平

2024-04-27 00:39:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要分析在过去一年中创建的与某个主题相关的Facebook组的数量,以及他们在同一时期的成员数量。你知道吗

目前,我遵循一个教程,使用以下代码为与该关键字相关的所有组搜索Facebook:

from selenium import webdriver

your_username = input("Please Enter Your Email/Login")
your_password = input("Please Enter Your Password")
query = input("Please enter a search query")

driver = webdriver.Chrome("C:\Python34\selenium\webdriver\chromedriver.exe")
print ("Logging in...")
driver.get("http://facebook.com")
driver.find_element_by_id("email").send_keys(your_username)
driver.find_element_by_id("pass").send_keys(your_password)
driver.find_element_by_id("loginbutton").click()
print ("Login Successful!")

driver.get("https://mobile.facebook.com/search/groups/?q=" + query)

import time
time.sleep(2) #Wait for page to load.

check = 0 #Variable to check after each pagination(Scroll Down)
last = 0 #What the last length of group_links was
time_to_sleep = 1 #Total time to sleep after each scroll down.
group_links = [] #A list to store new group links.
while check<10:
    elems = driver.find_elements_by_xpath("//a[@href]") # grabs every anchor element on page each loop
    for elem in elems: #Loops through each anchor element above
        new_link = elem.get_attribute("href") #grabs link from anchor element
        if "facebook.com/groups/" in new_link: #Checks to see if facebook group link
            if new_link not in group_links: #If new link found not already in our group links add it
                group_links.append(new_link)

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(time_to_sleep)  # Sleep here, let page scroll load
    if last == len(group_links): #If the amount of group links is the same as last time, then add 1 to check
        print ("Found Same Amount...") 
        check+=1
    else:#Check out http://www.pythonhowto.com
        check=0 #If not reset check back to 0
    last = len(group_links) #changes last to current length of group links
    print ("Total group links found => "),last

print ("Out of Loop")
filey = open("grouplinks.txt","w") #Open file
for link in group_links: #FOr each link found write it to file
    filey.write(link + "\n")
filey.close()
driver.quit() #Exits selenium driver (It can sometimes hang in background)

但是,这只给了我今天存在的组。有没有可能运行类似的程序来分析自2017年1月1日以来创建的组的数量?你知道吗

旁注:我已经读到,Facebook图形API是一种比scraping更有效的执行此类任务的方法。我应该换个方式吗? 最后,这是一个大学项目,最终我想要实现的是能够比较一段时间内与比特币相关的Facebook群组的数量,他们的会员资格,并将其与同期比特币的价格进行比较。你知道吗


Tags: toinnewfacebooktimecheckdrivergroup