python代码块 1 2 3 4 5 6 7 import requestsimport refrom selenium import webdriverimport timefrom selenium.webdriver.common.by import Byfrom threading import Thread from concurrent.futures import ThreadPoolExecutor
1.滚动页面到底部 1 2 3 js = 'window.scrollTo(0,document.body.scrollHeight)' driver.execute_script(js) time.sleep(5 )
2.自动翻页 1 2 3 4 5 6 7 for x in range (1 , 6 , 2 ): time.sleep(3 ) j = x / 9 js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j driver.execute_script(js)
3. selenium自动化 1 2 3 4 driver = webdriver.Chrome() driver.get('url' ) driver.maximize_window() driver.implicitly_wait(10 )
4.下载 1 2 3 4 5 6 7 8 9 def download (): i = 0 for url1 in video_url_list: resp_video = requests.get(url=url1).content title_list[i] = title_list[i].replace("/" , "-" ) with open ('ZHvideo\\' + title_list[i] + '.mp4' , mode='wb' ) as f: f.write(resp_video) print (title_list[i] + "下载完成!!!" ) i += 1
5.ui界面 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 main_url = [] def DaoHang (): app = QApplication([]) window = QMainWindow() window.resize(1000 , 100 ) window.move(500 , 310 ) window.setWindowTitle('导航栏' ) textEdit = QPlainTextEdit(window) textEdit.setPlaceholderText("请输入网址" ) textEdit.move(10 , 25 ) textEdit.resize(800 , 30 ) def show (): url = textEdit.toPlainText() main_url.append(url) print (main_url) window.close() button = QPushButton('确定' , window) button.clicked.connect(show) button.move(850 , 25 ) window.show() app.exec_() DaoHang()
6.下载进度条 1 2 3 4 5 6 7 8 9 10 11 12 13 from tqdm import tqdmresp_video = requests.get(url=video_url, stream=True ) file_size = int (resp_video.headers.get('Content-Length' )) pbar = tqdm(total=file_size) with open ("video\\" + title + ".mp4" , mode='wb' ) as f: for chunk in resp_video.iter_content(1024 *1024 *2 ): f.write(chunk) pbar.set_description('正在下载中...' ) pbar.update(1024 *1024 *2 ) pbar.set_description('下载完成!!!' ) pbar.close()
7.自动化读取表格 1 2 3 4 5 6 7 8 9 10 11 import xlrdimport osxlsx = xlrd.open_workbook('xl表格.xlsx' ) sheet = xlsx.sheet_by_index(0 ) data = sheet.cell_value(0 , 0 ) print (data)
8.写入表格 1 2 3 4 5 6 7 8 9 10 11 import xlwtimport osnewWorkbook = xlwt.Workbook('utf-8' ) Sheet = newWorkbook.add_sheet(sheetname='sheet1' ) Sheet.write(0 , 0 , '名字' ) newWorkbook.save('./xl表格.xlsx' )
版权声明: 此文章版权由“逸乐”所有,如有转载,请注明来自原作者