我需要分析去年创建的与某个主题相关的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)
然而,这只给了我今天的团体。是否可以运行类似的东西来分析自那以后创建的群组数量,比如说01/01/2017?
旁注:我已经读过,与刮擦相比,Facebook Graph API是一种更有效的执行此类任务的方法。我应该这样做吗? 最后;这是一个大学项目,最终我想要实现的是能够比较与比特币相关的Facebook群组的数量,他们在一段时间内的会员资格,并将其与比特币在同一时期的价格进行比较。 / p>