使用Python+Selenium類比登入QQ空間

來源:互聯網
上載者:User

爬QQ空間之類的頁面時大多需要進行登入,研究QQ登入規則的話,得分析大量Javascript的加密解密,這絕對能掉好幾斤頭髮。而現在有了selenium工具,麻麻再也不用擔心你的QQ登入!

Selenium是一個WEB自動化測試載入器,它運行時會直接執行個體化出一個瀏覽器,完全類比使用者的操作,比如點選連結、輸入表單,點擊按鈕提交等。所以我們使用它可以很方便的來登入QQ空間。
#匯入selenium2中的webdriver庫

from selenium import webdriver

#執行個體化出一個Firefox瀏覽器
driver = webdriver.Firefox()

#設定瀏覽器視窗的位置和大小
driver.set_window_position(20, 40)
driver.set_window_size(1100,700)

#開啟一個頁面(QQ空間登入頁)
driver.get('http://qzone.qq.com')
#登入表單在頁面的架構中,所以要切換到該架構
driver.switch_to_frame('login_frame')
#通過使用選取器選擇到表單元素進行類比輸入和點擊按鈕提交
driver.find_element_by_id('switcher_plogin').click()
driver.find_element_by_id('u').clear()
driver.find_element_by_id('u').send_keys('917464311')
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('123456')
driver.find_element_by_id('login_button').click()

#do something....
#退出視窗
driver.quit()

這樣就能方便的登入到QQ空間,下一步就可以利用這個登入狀態去抓取頁面內容或其它腦洞大開的應用了~~~
安裝selenium庫,建議直接使用pip工具

#pip install selenium

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.