selenium實現微博批量自動取關

2021-10-05 01:36:38 字數 2003 閱讀 7121

之前偶然登陸微博發現微博裡有60多個關注的物件,然而都不是自己關注的人(萬惡的微博 ?),然後又沒有發現全選 取關的按鈕,於是做了個自動幫助我們取關的指令碼,順便練習一下selenium庫的使用~

廢話不多說,上**~

from selenium import webdriver

import time

from selenium.webdriver import actionchains

browser = webdriver.chrome(

)#登陸

browser.get(

'')#//*[@style] 查詢所有包含style的所有元素,所有的屬性要加@

browser.find_element_by_xpath(

'//*[@id="loginname"]'

).clear(

)#輸入登入賬號

browser.find_element_by_xpath(

'//*[@id="loginname"]'

).send_keys(

'your id'

)browser.find_element_by_xpath(

'//*[@id="pl_login_form"]/div/div[3]/div[2]/div/input'

).clear(

)time.sleep(1)

#輸入登陸密碼

browser.find_element_by_xpath(

'//*[@id="pl_login_form"]/div/div[3]/div[2]/div/input'

).send_keys(

'your password'

)time.sleep(1)

browser.find_element_by_xpath(

'//*[@id="pl_login_form"]/div/div[3]/div[6]/a'

).click(

)time.sleep(1)

browser.find_element_by_xpath(

'//*[@id="v6_pl_rightmod_myinfo"]/div/div/div[2]/ul/li[1]/a/strong'

).click(

)time.sleep(1)

#browser.find_element_by_link_text("設定") 通過文字定位元素

#取關數量

for cou in

range(1

,10):

#定位懸停時的元素

actionchains(browser)

.move_to_element(browser.find_element_by_xpath(

'//*[@id="pl_official_relationmyfollow__95"]/div/div/div/div[3]/ul/li[1]/div[1]/div[2]/div[5]/p/a[3]/em'))

.perform(

) time.sleep(2)

browser.find_element_by_link_text(

"取消關注"

).click(

) time.sleep(1)

#browser.find_element_by_xpath('//*[@id="layer_15660574768511"]/div[2]/div[4]/a[1]').click()

browser.find_element_by_link_text(

"確定"

).click(

)browser.quit(

)

整個流程中的問題主要有兩個:

1.有乙個滑鼠懸停時才顯示的取消關注介面,使用actionchains(browser).move_to_element().perform()方法解決

2.最後還有乙個確定視窗跳出來,之前以為是彈窗,但是後來發現其實並不是,直接定位元素就可以了

實現微博批量取消關注使用者

為了實現批量取關微博使用者,寫了個小東西 僅供參考 1,js userscript name 批量選擇微博關注列表 namespace version 0.1 description try to take over the world author you match grant none use...

微博批量點讚

微博批量點讚 import requests class weibospider def init self,username,password self.session requests.session self.headers self.session.headers.update self.h...

selenium自動登入登入新浪微博(一)

接上個文章,因為失敗,思考了一下原因,參考了其他的博主。使用cookie可以免密碼登入 所以自動化登入微博首先需要獲取已經登入微博後瀏覽器的cookie。接下來上 匯入需要的資料報 from selenium import webdriver import time 將獲取的cookie儲存在jso...