<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          分享十個好用到爆的Python自動化腳本

          共 12712字,需瀏覽 26分鐘

           ·

          2022-12-22 01:13

          在這個自動化時代,我們有很多重復無聊的工作要做。想想這些你不再需要一次又一次地做的無聊的事情,讓它自動化,讓你的生活更輕松。那么在本文中,我將向您介紹10個Python自動化腳本,以使你的工作更加自動化,生活更加輕松。

          01、解析和提取 HTML

          此自動化腳本將幫助你從網(wǎng)頁URL中提取HTML,然后還為你提供可用于解析HTML以獲取數(shù)據(jù)的功能。這個很棒的腳本對于網(wǎng)絡爬蟲和那些想要解析HTML以獲取重要數(shù)據(jù)的人來說是一種很好的享受。

          # 解析和提取 HTML 
          # pip install gazpacho
          進口西班牙涼菜湯
          # 從 URL 中提取 HTML 
          url = ' https://www.example.com/'
           html = gazpacho.get(url) 
          print(html)
          # 提取帶有標題的 HTML 
          headers = {'User-Agent''Mozilla/5.0'
          html = gazpacho.get(url, headers=headers) 
          print(html)
          # 解析 HTML
          解析 = gazpacho.Soup(html)
          # 查找單個標簽
          tag1 = parse.find('h1'
          tag2 = parse.find('span')
          # 查找多個標簽
          tags1 = parse.find_all('p'
          tags2 = parse.find_all('a')
          # 按類查找標簽
          tag = parse.find('.class')
          # 按屬性查找標簽
          tag = parse.find("div", attrs={"class""test"})
          # 從標簽中提取文本
          text = parse.find('h1').text 
          text = parse.find_all('p')[0].text

          02、二維碼掃描儀

          擁有大量二維碼圖像或只想掃描二維碼圖像,那么此自動化腳本將幫助你。該腳本使用Qrtools模塊,使你能夠以編程方式掃描QR圖像。

          # 二維碼掃描儀
          # pip install qrtools
          從 qrtools 導入 Qr
          def Scan_Qr(qr_img): 
              qr = Qr() 
              qr.decode(qr_img) 
              print(qr.data) 
              return qr.data
          print("您的二維碼是:", Scan_Qr("qr.png"))

          03、截圖

          現(xiàn)在,你可以使用下面這個很棒的腳本以編程方式截取屏幕截圖。使用此腳本,你可以直接截屏或截取特定區(qū)域的屏幕截圖。

          # 抓取屏幕截圖
          # pip install pyautogui 
          # pip install Pillow
          from pyautogui import screenshot 
          import time 
          from PIL import ImageGrab
          # 抓取屏幕截圖
          def grab_screenshot(): 
              shot = screenshot() 
              shot.save('my_screenshot.png')
          # 抓取特定區(qū)域的截圖
          def grab_screenshot_area(): 
              area = (0, 0, 500, 500) 
              shot = ImageGrab.grab(area) 
              shot.save('my_screenshot_area.png')
          # 延遲截屏
          def grab_screenshot_delay(): time.sleep 
              (5) 
              shot = screenshot() 
              shot.save('my_screenshot_delay.png')

          04、創(chuàng)建有聲讀物

          厭倦了手動將您的PDF書籍轉換為有聲讀物,那么這是你的自動化腳本,它使用 GTTS模塊將你的PDF文本轉換為音頻。

          # 創(chuàng)建有聲讀物
          # pip install gTTS 
          # pip install PyPDF2
          from PyPDF2 import PdfFileReader as reader
          from gtts import gTTS
          def create_audio(pdf_file): 
              read_Pdf = reader(open(pdf_file, 'rb')) 
              for page in range(read_Pdf.numPages): 
                  text = read_Pdf.getPage(page).extractText() 
                  tts = gTTS(text, lang=' en'
                  tts.save('page' + str(page) + '.mp3')
          create_audio('book.pdf')

          05、PDF 編輯器

          使用以下自動化腳本使用Python編輯PDF文件。該腳本使用PyPDF4模塊,它是PyPDF2的升級版本,下面我編寫了Parse TextRemove pages等常用功能。

          當你有大量PDF文件要編輯或需要以編程方式在Python項目中使用腳本時,這是一個方便的腳本。

          # PDF 編輯器
          # pip install PyPDf4
          import PyPDF4
          # 從 PDF 解析文本
          def parse_text(pdf_file): 
              reader = PyPDF4.PdfFileReader(pdf_file) 
              for page in reader.pages: 
                  print(page.extractText())
          # 從 PDF 中刪除頁面
          def remove_page(pdf_file, page_numbers): 
              filer = PyPDF4.PdfReader('source.pdf''rb'
              out = PyPDF4.PdfWriter() 
              for index in page_numbers: 
                  page = filer.pages[index] 
                  out .add_page(頁面)
          用 open('rm.pdf''wb') as f: 
                  out.write(f)
          # 添加空白頁到 PDF 
          def add_page(pdf_file, page_number): 
              reader = PyPDF4.PdfFileReader(pdf_file) 
              writer = PyPDF4.PdfWriter() 
              writer.addPage() 
              with open('add.pdf''wb') as f: 
                  writer.write(f)
          # 旋轉頁面
          def rotate_page(pdf_file): 
              reader = PyPDF4.PdfFileReader(pdf_file) 
              writer = PyPDF4.PdfWriter() 
              for page in reader.pages: 
                  page.rotateClockwise(90) 
                  writer.addPage(page) 
              with open('rotate.pdf ''wb') as f: 
                  writer.write(f)
          # 合并 PDF 
          def merge_pdfs(pdf_file1, pdf_file2): 
              pdf1 = PyPDF4.PdfFileReader(pdf_file1) 
              pdf2 = PyPDF4.PdfFileReader(pdf_file2) 
              writer = PyPDF4.PdfWriter() 
              for page in pdf1.pages: 
                  writer.addPage(page) 
              for page in pdf2.pages: 
                  writer.addPage(page) 
              with open('merge.pdf''wb') as f: 
                  writer.write(f)

          06、迷你 Stackoverflow

          作為一名程序員,我知道我們每天都需要StackOverflow,但你不再需要在Google上搜索它。現(xiàn)在,在您繼續(xù)處理項目的同時,在你的CMD中獲得直接解決方案。通過使用Howdoi模塊,你可以在命令提示符或終端中獲得StackOverflow解決方案。你可以在下面找到一些可以嘗試的示例。

          # Automate Stackoverflow
          # pip install howdoi
          # Get Answers in CMD
          #example 1
          > howdoi how do i install python3
          # example 2
          > howdoi selenium Enter keys
          # example 3
          > howdoi how to install modules
          # example 4
          > howdoi Parse html with python
          # example 5
          > howdoi int not iterable error
          # example 6
          > howdoi how to parse pdf with python
          # example 7
          > howdoi Sort list in python
          # example 8
          > howdoi merge two lists in python
          # example 9
          >howdoi get last element in list python
          # example 10
          > howdoi fast way to sort list

          07、自動化手機

          您可以了解有關 ADB的更多信息,并探索更多令人興奮的方法來實現(xiàn)手機自動化,讓您的生活更輕松。

          # 自動化手機
          # pip install opencv-python
          import subprocess
          def main_adb(cm): 
              p = subprocess.Popen(cm.split(''), stdout=subprocess.PIPE, shell=True) 
              (output, _) = p.communicate() 
              return output.decode('utf-8 ')
          # Swipe 
          def swipe(x1, y1, x2, y2, duration): 
              cmd = 'adb shell input swipe {} {} {} {} {}'.format(x1, y1, x2, y2, duration) 
              return main_adb(命令)
          # 點擊或點擊
          def tap(x, y): 
              cmd = 'adb shell input tap {} {}'.format(x, y) 
              return main_adb(cmd)
          # 撥打電話
          def make_call(number): 
              cmd = f"adb shell am start -a android.intent.action.CALL -d tel:{number}" 
              return main_adb(cmd)
          # 發(fā)送短信
          def send_sms(number, message): 
              cmd = 'adb shell am start -a android.intent.action.SENDTO -d sms:{} --es sms_body "{}"'.format(number, message) 
              return main_adb(cmd)
          # 從手機下載文件到電腦
          def download_file(file_name): 
              cmd = 'adb pull /sdcard/{}'.format(file_name) 
              return main_adb(cmd)
          # 截圖
          def screenshot(): 
              cmd = 'adb shell screencap -p' 
              return main_adb(cmd)
          # 開機關機
          def power_off(): 
              cmd = '"adb shell input keyevent 26"' 
              return main_adb(cmd)

          08、監(jiān)控 CPU/GPU 溫度

          你可能使用CPU-Z或任何規(guī)格監(jiān)控軟件來捕獲你的Cpu 和Gpu溫度,但你也可以通過編程方式進行。好吧,這個腳本使用PythonnetOpenhardwareMonitor來幫助你監(jiān)控當前的CpuGpu溫度。

          你可以使用它在達到一定溫度時通知自己,也可以在Python項目中使用它來簡化日常生活。

          # 獲取 CPU/GPU 溫度
          # pip install pythonnet
          從 OpenHardwareMonitorLib導入 clr 
          clr.AddReference("OpenHardwareMonitorLib"
          *
          spec = Computer() 
          spec.GPUEnabled = True 
          spec.CPUEnabled = True 
          spec.Open()
          # Get CPU Temp 
          def Cpu_Temp(): 
              while True: 
                  for cpu in range(0, len(spec.Hardware[0].Sensors)): 
                      if "/temperature" in str(spec.Hardware[0].Sensors[cpu ].Identifier): 
                          print(str(spec.Hardware[0].Sensors[cpu].Value))
          # Get GPU Temp 
          def Gpu_Temp() 
              while True: 
                  for gpu in range(0, len(spec.Hardware[0].Sensors)): 
                      if "/temperature" in str(spec.Hardware[0].Sensors[gpu] .Identifier): 
                          print(str(spec.Hardware[0].Sensors[gpu].Value))

          09、Instagram 上傳機器人

          Instagram是一個著名的社交媒體平臺,你現(xiàn)在不需要通過智能手機上傳照片或視頻。你可以使用以下腳本以編程方式執(zhí)行此操作。

          # 在 Insta 上上傳照片和視頻
          # pip install instabot
          從 instabot 導入 Bot
          def Upload_Photo(img): 
              robot = Bot() 
              robot.login(username="user", password="pass"
              robot.upload_photo(img, caption="Medium Article"
              print("照片上傳")
          def Upload_Video(video): 
              robot = Bot() 
              robot.login(username="user", password="pass"
              robot.upload_video(video, caption="Medium Article"
              print("Video Uploaded")
          def Upload_Story(img): 
              robot = Bot() 
              robot.login(username="user", password="pass"
              robot.upload_story(img, caption="Medium Article"
              print("Story Photos Uploaded")
          Upload_Photo("img.jpg"
          Upload_Video("video.mp4")

          10、視頻水印

          使用此自動化腳本為你的視頻添加水印,該腳本使用Moviepy,這是一個方便的視頻編輯模塊。在下面的腳本中,你可以看到如何添加水印并且可以自由使用它。

          # 使用 Python 制作視頻水印
          # pip install moviepy
          從moviepy.editor導入*
          clip = VideoFileClip("myvideo.mp4", audio=True) 
          width,height = clip.size   
          text = TextClip("WaterMark", font='Arial', color='white', fontsize=28)
          set_color = text.on_color(size=(clip.w + text.w, text.h-10), color=(0,0,0), pos=(6,'center'), col_opacity=0.6) 
          set_textPos = set_color.set_pos( lambda pos: (max(width/30,int(width-0.5* width* pos)),max(5*height/6,int(100* pos))) )
          Output = CompositeVideoClip([clip, set_textPos]) 
          Output.duration = clip.duration 
          Output.write_videofile("output.mp4", fps=30, codec='libx264')


          瀏覽 41
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产内射免费观看视频 | 亚洲黄色五月天 | 亚洲免费情毛片 | 婷婷色视频在线观看 | 我爱大香蕉伊人久久 |