Python實現對百度雲的檔案上傳,

來源:互聯網
上載者:User

Python實現對百度雲的檔案上傳,

環境準備

python3.6

PyCharm 2017.1.3

Windows環境

架構搭建

selenium3.6

安裝方法:

pip install selenium

實現步驟:

一、步驟分析

1、選擇“帳號密碼登入”

2、使用者名稱、密碼輸入,登入

3、檔案上傳

註:本文主要介紹利用selenium包下的webdriver載入Firefox瀏覽器。

二、元素捕捉

利用Firefox瀏覽器firebug外掛程式複製控制項的XPATH路徑,註:Python3.6對應Firefox版本40.x,暫不支援最新版本50.x。

1、點擊“帳號密碼登入”,擷取其源檔案

如下:

點擊右鍵,複製Xpath路徑:/html/body/div[1]/div[3]/div[6]/div/div[6]/div[2]/a

登入按鈕和檔案上傳同上,擷取其相應的Xpath路徑

代碼:

1 #選擇帳號密碼登入2 driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[6]/div[2]/a').click()3 # 登入  4 driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[5]/input').send_keys('username')5 driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[6]/input').send_keys('password')6 driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[9]/input').click()

2、登入成功後,點擊檔案上傳,彈出檔案對話方塊

“上傳”的Xpath路徑為://*[@id="h5Input0"]

代碼:

1 #上傳2 driver.find_element_by_xpath('//*[@id="h5Input0"]').click()

點擊上傳按鈕,彈出檔案對話方塊

三、AutoIT編寫指令碼實現上傳檔案

webdriver無法對檔案直接進行操作,所以需要藉助AutoIT來實現檔案上傳

AutoIT:https://www.autoitscript.com/site

安裝AutoIt之後,開啟AutoIt Window Info(x64) 
 

4、擷取檔案上傳視窗的控制項資訊:

開啟autoit工具之後,用滑鼠將Finder Tool的表徵圖拖到要識別的控制項上

5、編寫AutoIt指令碼,實現檔案上傳

  3. 將檔案儲存upfile.au3 

  4. 使用compile script to exe將上述AutoIt指令碼編譯為exe檔案供python指令碼調用 

6、最後,使用Python指令碼調用AutoIT指令碼

1 #點擊上傳,開啟上傳檔案視窗2 driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[2]/div[2]/a[1]/form/input').click()3 4 #使用autoit指令碼自動上傳檔案5 #需要匯入python的os庫檔案:  import os6 os.system("D:/upfile.exe")

完整代碼如下:

 1 import os 2 from selenium import webdriver 3 import time 4 class Connect(): 5     def __init__(self,UserName,PassWord,URL): 6         self.UserName = UserName 7         self.PassWord = PassWord 8         self.URL = URL 9     def connect(self):10         self.driver = webdriver.Firefox()11         self.driver.get(self.URL)12         self.driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[6]/div[2]/a').click()13         self.driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[5]/input').send_keys(self.UserName)14         self.driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[6]/input').send_keys(self.PassWord)15         self.driver.find_element_by_xpath('/html/body/div[1]/div[3]/div[6]/div/div[3]/form/p[9]/input').click()16         #設定考慮時間17         time.sleep(30)18         sreach_window = self.driver.current_window_handle  # 此行代碼用來定位當前頁面19         self.driver.find_element_by_xpath('//*[@id="h5Input0"]').click()20         os.system(r"C:\Users\zg\Desktop\upfile.exe")21 Connect(UserName,PassWord,URL).upload()
View Code

本文參考文章:http://blog.csdn.net/justheretobe/article/details/50939021

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.