自動化爬蟲方法和庫很多,難點大多數在登錄。可以大致分為:普通驗證碼,掃碼登錄,QQ一鍵登錄,拖動驗證,無痕驗證,人工識別(比如12306登錄)
萬能大法可以破解一切以上需求,自動控制瀏覽器行為
參考文獻:
https://www.jianshu.com/p/1531e12f8852
https://blog.csdn.net/sinat_41774836/article/details/88965281
直接貼上我的代碼
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By import time import os class upload(object): #打開瀏覽器 def __init__(self): self.videopwd = 'F:\\腳本\\douyin\\douyindata2' self.videolist = os.listdir(self.videopwd) self.driver = webdriver.Chrome() self.driver.get("https://不告訴你.com") #等待10秒,微信掃碼登錄后繼續操作 time.sleep(10) #執行上傳 def uploadbtn(self,file): #定位上傳按鈕,添加本地文件 wait_find_element=WebDriverWait(self.driver,8).until(EC.presence_of_element_located((By.CLASS_NAME, "file-input"))) wait_find_element.send_keys(file) time.sleep(8) #點擊一鍵發布 wait_find_element=WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.CLASS_NAME, "btn-right"))) wait_find_element.click() print('上傳',file,'結束') #重命名 def renamefile(self,file): newvideo = video.replace('@', '') newvideo = newvideo.replace('#我的觀影報告', '') newvideo = newvideo.replace('#', '') newvideo = newvideo.replace('抖音', '') newvideo = newvideo.replace('小助手', '') newvideo = newvideo.replace('😂😂😂', '') newvideo = newvideo.replace(' ', '') srcfile = os.path.join(self.videopwd, video) dstfile = os.path.join(self.videopwd, newvideo) try: os.rename(srcfile, dstfile) return (dstfile) except: print(srcfile,'文件重命名失敗') #刪除文件 def delfile(self,file): if os.path.exists(file): os.remove(file) if os.path.exists(file): print('刪除',file,'失敗') else: print('刪除',file,'成功') if __name__=='__main__': pq=upload() for video in pq.videolist: if 'mp4' in video: #重命名 file=pq.renamefile(video) #執行上傳 pq.uploadbtn(file) #刪除視頻 pq.delfile(file) time.sleep(5)
最后成功: