简体   繁体   English

用python实现Facebook帐户下载自动化?

[英]Facebook account download automation with python?

As a regular task within my job role i often have to download full Facebook accounts from within a users account. 作为我工作职责中的一项常规任务,我经常必须从用户帐户中下载完整的Facebook帐户。 I am trying to improve my workflow and automate this if i can. 我正在尝试改善我的工作流程,并在可能的情况下使其自动化。

I have tried searching for this topic on the site and although many cover the login part i am yet to locate a question that deals with the popup windows of Facebook. 我曾尝试在网站上搜索此主题,尽管许多内容涵盖了登录部分,但我还没有找到解决Facebook弹出窗口的问题。 If i am wrong i apologise and please amend the post accordingly. 如果我错了,我深表歉意,请相应地修改帖子。

As a starting point i have decided to start learning python and am using this to script the process with a little help from selenium and Chrome Driver. 首先,我决定开始学习python,并在selenium和Chrome Driver的少许帮助下使用它来编写过程脚本。 I have managed to write the code to login and navigate to the correct page and click the initial link 'download a copy'. 我设法编写了代码以登录并导航到正确的页面,然后单击初始链接“下载副本”。 I am struggling however to get the script to locate and click the 'Start My Archive' button within the popup window. 但是,我很难使脚本找到并单击弹出窗口中的“开始我的档案”按钮。

Here is the code that i have used so far including the several alternative code blocks that i have tried commented out at the bottom: 这是我到目前为止使用的代码,包括我尝试在底部注释掉的几个替代代码块:

import time

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

#Global Variables
target = "john.doe.7"
username = "john@doe.com"
password = "Password"
sleep = 5

#Finds and locates the ChromeDriver
driver = webdriver.Chrome("C:\Python35-32\ChromeDriver\chromedriver.exe")

#Set Chrome Options
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

#Directs browser to webpage
driver.get('http://www.facebook.com');

#code block to log in user
def logmein():
    search_box = driver.find_element_by_name('email')
    search_box.send_keys(username)
    search_box = driver.find_element_by_name('pass')
    search_box.send_keys(password)
    search_box.submit()

#code to go to specific URL
def GoToURL(URL):
    time.sleep(sleep) # Let the user actually see something!
    driver.get(URL);    

logmein()

GoToURL('https://www.facebook.com/'+'settings')

link = driver.find_element_by_link_text('Download a copy')
link.click()

#driver.find_element_by.xpath("//button[contains(., 'Start My Archive')]").click()

#driver.find_element_by_css_selector('button._42ft._42fu.selected._42gz._42gy').click()

#driver.find_element_by_xpath("//*[contains(text(), 'Start My Archive')]").click()

#driver.find_element_by_css_selector('button._42ft._42fu.layerConfirm.uiOverlayButton.selected._42g-._42gy').click()

#from selenium.webdriver.common.action_chains.ActionChains import driver

#buttons = driver.find_elements_by_xpath("//title[contains(text(),'Start My Archive')]")
#actions = ActionChains(self.driver)
#time.sleep(2)
#actions.click(button)
#actions.perform()

Just add this to your code and run if it is working or not. 只需将其添加到您的代码中,然后运行即可(如果无法运行)。 Always use CssSelector. 始终使用CssSelector。

Look i ran the below code in eclipse with java and I'm not aware of python so if something is wrong in syntax , i apologies. 看起来我用Java在eclipse中运行了以下代码,但我不了解python,因此如果语法有误,我深表歉意。 Just Try this and see. 只需尝试一下,看看。

driver.implicitly_wait(10)
Startmyarchive = driver.find_element_by_css_selector("._42ft._42fu.selected._42gz._42gy")
Startmyarchive.click()

driver.implicitly_wait(10)
Acknowledge = driver.find_element_by_css_selector("._42ft._42fu.layerConfirm.uiOverlayButton.selected._42g-._42gy")
Acknowledge.click()

driver.implicitly_wait(10)
ClickOkay = driver.find_element_by_css_selector("._42ft._42fu.layerCancel.uiOverlayButton.selected._42g-._42gy")
ClickOkay.click()

Happy Learning :-) Do reply back for any query. 祝您学习愉快:-)如有任何疑问,请回复。

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

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