[Python] 10个Python脚本来自动化你的日常任务

 
2653 3
小明Python 2022-10-11 14:41:14 | 显示全部楼层 |阅读模式
在这个自动化时代,我们有很多重复无聊的工作要做。想想这些你不再需要一次又一次地做的无聊的事情,让它自动化,让你的生活更轻松。那么在本文中,我将向您介绍 10 个 Python 自动化脚本,以使你的工作更加自动化,生活更加轻松。因此,没有更多的重复任务将这篇文章放在您的列表中,让我们开始吧。

01、解析和提取 HTML

此自动化脚本将帮助你从网页 URL 中提取 HTML,然后还为你提供可用于解析 HTML 以获取数据的功能。这个很棒的脚本对于网络爬虫和那些想要解析 HTML 以获取重要数据的人来说是一种很好的享受。
  1. # Parse and Extract HTML
  2. # pip install gazpacho
  3. import gazpacho
  4. # Extract HTML from URL
  5. url = 'https://www.example.com/'
  6. html = gazpacho.get(url)
  7. print(html)
  8. # Extract HTML with Headers
  9. headers = {'User-Agent': 'Mozilla/5.0'}
  10. html = gazpacho.get(url, headers=headers)
  11. print(html)
  12. # Parse HTML
  13. parse = gazpacho.Soup(html)
  14. # Find single tags
  15. tag1 = parse.find('h1')
  16. tag2 = parse.find('span')
  17. # Find multiple tags
  18. tags1 = parse.find_all('p')
  19. tags2 = parse.find_all('a')
  20. # Find tags by class
  21. tag = parse.find('.class')
  22. # Find tags by Attribute
  23. tag = parse.find("div", attrs={"class": "test"})
  24. # Extract text from tags
  25. text = parse.find('h1').text
  26. text = parse.find_all('p')[0].text
复制代码
02、二维码扫描仪

拥有大量二维码图像或只想扫描二维码图像,那么此自动化脚本将帮助你。该脚本使用 Qrtools 模块,使你能够以编程方式扫描 QR 图像。
  1. # Qrcode Scanner
  2. # pip install qrtools
  3. from qrtools import Qr
  4. def Scan_Qr(qr_img):
  5.     qr = Qr()
  6.     qr.decode(qr_img)
  7.     print(qr.data)
  8.     return qr.data
  9. print("Your Qr Code is: ", Scan_Qr("qr.png"))
复制代码
03、截图

现在,你可以使用下面这个很棒的脚本以编程方式截取屏幕截图。使用此脚本,你可以直接截屏或截取特定区域的屏幕截图。

  1. # Grab Screenshot
  2. # pip install pyautogui
  3. # pip install Pillow
  4. from pyautogui import screenshot
  5. import time
  6. from PIL import ImageGrab
  7. # Grab Screenshot of Screen
  8. def grab_screenshot():
  9.     shot = screenshot()
  10.     shot.save('my_screenshot.png')
  11. # Grab Screenshot of Specific Area
  12. def grab_screenshot_area():
  13.     area = (0, 0, 500, 500)
  14.     shot = ImageGrab.grab(area)
  15.     shot.save('my_screenshot_area.png')
  16. # Grab Screenshot with Delay
  17. def grab_screenshot_delay():
  18.     time.sleep(5)
  19.     shot = screenshot()
  20.     shot.save('my_screenshot_delay.png')
复制代码
04、创建有声读物

厌倦了手动将您的 PDF 书籍转换为有声读物,那么这是你的自动化脚本,它使用 GTTS 模块将你的 PDF 文本转换为音频。
  1. # Create Audiobooks
  2. # pip install gTTS
  3. # pip install PyPDF2
  4. from PyPDF2 import PdfFileReader as reader<div class="blockcode"><blockquote># PDF Editor
  5. # pip install PyPDf4
  6. import PyPDF4
  7. # Parse the Text from PDF
  8. def parse_text(pdf_file):
  9.     reader = PyPDF4.PdfFileReader(pdf_file)
  10.     for page in reader.pages:
  11.         print(page.extractText())
  12. # Remove Page from PDF
  13. def remove_page(pdf_file, page_numbers):
  14.     filer = PyPDF4.PdfReader('source.pdf', 'rb')
  15.     out = PyPDF4.PdfWriter()
  16.     for index in page_numbers:
  17.         page = filer.pages[index]
  18.         out.add_page(page)
  19. with open('rm.pdf', 'wb') as f:
  20.         out.write(f)
  21. # Add Blank Page to PDF
  22. def add_page(pdf_file, page_number):
  23.     reader = PyPDF4.PdfFileReader(pdf_file)
  24.     writer = PyPDF4.PdfWriter()
  25.     writer.addPage()
  26.     with open('add.pdf', 'wb') as f:
  27.         writer.write(f)
  28. # Rotate Pages
  29. def rotate_page(pdf_file):
  30.     reader = PyPDF4.PdfFileReader(pdf_file)
  31.     writer = PyPDF4.PdfWriter()
  32.     for page in reader.pages:
  33.         page.rotateClockwise(90)
  34.         writer.addPage(page)
  35.     with open('rotate.pdf', 'wb') as f:
  36.         writer.write(f)
  37. # Merge PDFs
  38. def merge_pdfs(pdf_file1, pdf_file2):
  39.     pdf1 = PyPDF4.PdfFileReader(pdf_file1)
  40.     pdf2 = PyPDF4.PdfFileReader(pdf_file2)
  41.     writer = PyPDF4.PdfWriter()
  42.     for page in pdf1.pages:
  43.         writer.addPage(page)
  44.     for page in pdf2.pages:
  45.         writer.addPage(page)
  46. Python学习交流群: 748989764
  47.     with open('merge.pdf', 'wb') as f:
  48.         writer.write(f)
复制代码

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 Text、Remove pages 等常用功能。

当你有大量 PDF 文件要编辑或需要以编程方式在 Python 项目中使用脚本时,这是一个方便的脚本。
  1. # PDF Editor
  2. # pip install PyPDf4
  3. import PyPDF4
  4. # Parse the Text from PDF
  5. def parse_text(pdf_file):
  6. reader = PyPDF4.PdfFileReader(pdf_file)
  7. for page in reader.pages:
  8. print(page.extractText())
  9. # Remove Page from PDF
  10. def remove_page(pdf_file, page_numbers):
  11. filer = PyPDF4.PdfReader('source.pdf', 'rb')
  12. out = PyPDF4.PdfWriter()
  13. for index in page_numbers:
  14. page = filer.pages[index]
  15. out.add_page(page)
  16. with open('rm.pdf', 'wb') as f:
  17. out.write(f)
  18. # Add Blank Page to PDF
  19. def add_page(pdf_file, page_number):
  20. reader = PyPDF4.PdfFileReader(pdf_file)
  21. writer = PyPDF4.PdfWriter()
  22. writer.addPage()
  23. with open('add.pdf', 'wb') as f:
  24. writer.write(f)
  25. # Rotate Pages
  26. def rotate_page(pdf_file):
  27. reader = PyPDF4.PdfFileReader(pdf_file)
  28. writer = PyPDF4.PdfWriter()
  29. for page in reader.pages:
  30. page.rotateClockwise(90)
  31. writer.addPage(page)
  32. with open('rotate.pdf', 'wb') as f:
  33. writer.write(f)
  34. # Merge PDFs
  35. def merge_pdfs(pdf_file1, pdf_file2):
  36. pdf1 = PyPDF4.PdfFileReader(pdf_file1)
  37. pdf2 = PyPDF4.PdfFileReader(pdf_file2)
  38. writer = PyPDF4.PdfWriter()
  39. for page in pdf1.pages:
  40. writer.addPage(page)
  41. for page in pdf2.pages:
  42. writer.addPage(page)
  43. Python学习交流群: 748989764
  44. with open('merge.pdf', 'wb') as f:
  45. writer.write(f)
复制代码
06、迷你 Stackoverflow

作为一名程序员,我知道我们每天都需要 StackOverflow,但你不再需要在 Google 上搜索它。现在,在您继续处理项目的同时,在你的 CMD 中获得直接解决方案。通过使用 Howdoi 模块,你可以在命令提示符或终端中获得 StackOverflow 解决方案。你可以在下面找到一些可以尝试的示例。

  1. # Automate Stackoverflow
  2. # pip install howdoi
  3. # Get Answers in CMD
  4. #example 1
  5. > howdoi how do i install python3
  6. # example 2
  7. > howdoi selenium Enter keys
  8. # example 3
  9. > howdoi how to install modules
  10. # example 4
  11. > howdoi Parse html with python
  12. # example 5
  13. > howdoi int not iterable error
  14. # example 6
  15. > howdoi how to parse pdf with python
  16. # example 7
  17. > howdoi Sort list in python
  18. # example 8
  19. > howdoi merge two lists in python
  20. # example 9
  21. >howdoi get last element in list python
  22. # example 10
  23. > howdoi fast way to sort list
复制代码
07、自动化手机
此自动化脚本将帮助你使用 Python 中的 Android 调试桥 (ADB) 自动化你的智能手机。下面我将展示如何自动执行常见任务,例如滑动手势、呼叫、发送短信等等。

您可以了解有关 ADB 的更多信息,并探索更多令人兴奋的方法来实现手机自动化,让您的生活更轻松。
  1. # Automate Mobile Phones
  2. # pip install opencv-python
  3. import subprocess
  4. def main_adb(cm):
  5.     p = subprocess.Popen(cm.split(' '), stdout=subprocess.PIPE, shell=True)
  6.     (output, _) = p.communicate()
  7.     return output.decode('utf-8')
  8. # Swipe
  9. def swipe(x1, y1, x2, y2, duration):
  10.     cmd = 'adb shell input swipe {} {} {} {} {}'.format(x1, y1, x2, y2, duration)
  11.     return main_adb(cmd)
  12. # Tap or Clicking
  13. def tap(x, y):
  14.     cmd = 'adb shell input tap {} {}'.format(x, y)
  15.     return main_adb(cmd)
  16. # Make a Call
  17. def make_call(number):
  18.     cmd = f"adb shell am start -a android.intent.action.CALL -d tel:{number}"
  19.     return main_adb(cmd)
  20. # Send SMS
  21. def send_sms(number, message):
  22.     cmd = 'adb shell am start -a android.intent.action.SENDTO -d  sms:{} --es sms_body "{}"'.format(number, message)
  23.     return main_adb(cmd)
  24. # Download File From Mobile to PC
  25. def download_file(file_name):
  26.     cmd = 'adb pull /sdcard/{}'.format(file_name)
  27.     return main_adb(cmd)
  28. # Take a screenshot
  29. def screenshot():
  30.     cmd = 'adb shell screencap -p'
  31.     return main_adb(cmd)
  32. # Power On and Off
  33. def power_off():  Python学习群:748989764
  34.     cmd = '"adb shell input keyevent 26"'
  35.     return main_adb(cmd)
复制代码
08、监控 CPU/GPU 温度


你可能使用 CPU-Z 或任何规格监控软件来捕获你的 Cpu 和 Gpu 温度,但你也可以通过编程方式进行。好吧,这个脚本使用 Pythonnet 和 OpenhardwareMonitor 来帮助你监控当前的 Cpu 和 Gpu 温度。

你可以使用它在达到一定温度时通知自己,也可以在 Python 项目中使用它来简化日常生活。
  1. # Get CPU/GPU Temperature
  2. # pip install pythonnet
  3. import clr
  4. clr.AddReference("OpenHardwareMonitorLib")
  5. from OpenHardwareMonitorLib import *
  6. spec = Computer()
  7. spec.GPUEnabled = True
  8. spec.CPUEnabled = True
  9. spec.Open()
  10. # Get CPU Temp
  11. def Cpu_Temp():
  12.     while True:
  13.         for cpu in range(0, len(spec.Hardware[0].Sensors)):
  14.             if "/temperature" in str(spec.Hardware[0].Sensors[cpu].Identifier):
  15.                 print(str(spec.Hardware[0].Sensors[cpu].Value))
  16. # Get GPU Temp
  17. def Gpu_Temp()
  18.     while True:
  19.         for gpu in range(0, len(spec.Hardware[0].Sensors)):
  20.             if "/temperature" in str(spec.Hardware[0].Sensors[gpu].Identifier):
  21.                 print(str(spec.Hardware[0].Sensors[gpu].Value))
复制代码


09、Instagram 上传机器人


Instagram 是一个著名的社交媒体平台,你现在不需要通过智能手机上传照片或视频。你可以使用以下脚本以编程方式执行此操作。
  1. # Upload Photos and Video on Insta
  2. # pip install instabot
  3. from instabot import Bot
  4. def Upload_Photo(img):
  5.     robot = Bot()
  6.     robot.login(username="user", password="pass")
  7.     robot.upload_photo(img, caption="Medium Article")
  8.     print("Photo Uploaded")
  9. def Upload_Video(video):
  10.     robot = Bot()
  11.     robot.login(username="user", password="pass")
  12.     robot.upload_video(video, caption="Medium Article")
  13.     print("Video Uploaded")
  14. def Upload_Story(img):
  15.     robot = Bot()
  16.     robot.login(username="user", password="pass")
  17.     robot.upload_story(img, caption="Medium Article")
  18.     print("Story Photos Uploaded")
  19. Upload_Photo("img.jpg")
  20. Upload_Video("video.mp4")
复制代码

10、视频水印

使用此自动化脚本为你的视频添加水印,该脚本使用 Moviepy,这是一个方便的视频编辑模块。在下面的脚本中,你可以看到如何添加水印并且可以自由使用它。
  1. # Video Watermark with Python
  2. # pip install moviepy
  3. from moviepy.editor import *
  4. clip = VideoFileClip("myvideo.mp4", audio=True)
  5. width,height = clip.size  
  6. text = TextClip("WaterMark", font='Arial', color='white', fontsize=28)
  7. set_color = text.on_color(size=(clip.w + text.w, text.h-10), color=(0,0,0), pos=(6,'center'), col_opacity=0.6)
  8. set_textPos = set_color.set_pos( lambda pos: (max(width/30,int(width-0.5* width* pos)),max(5*height/6,int(100* pos))) )
  9. Output = CompositeVideoClip([clip, set_textPos])
  10. Output.duration = clip.duration
  11. Output.write_videofile("output.mp4", fps=30, codec='libx264')
复制代码
最后的想法


希望你能找到一些新的有趣的东西来让你的日常任务自动化。如果你喜欢这篇文章,请不要忘记与你的朋友分享它,也请你点赞我,关注我,如果有任何问题,请在留言区给我留言,感谢你的阅读,祝生活愉快!







小远实名认证 2022-10-12 15:57:38 来自手机 | 显示全部楼层
我觉得很好  给你点个赞
454771774 2022-10-12 16:27:23 | 显示全部楼层
你好 有大神接单吗 预算高 海外手机全自动抢购爬虫  全自动监控库存 有库存自动下单 付款 最后难点总被风控不能店取 只能大半个月之后邮寄 有大神能攻克吗 急求 预算高 联系v shanbiangui
小明Python 2022-10-12 16:32:14 | 显示全部楼层
小远 发表于 2022-10-12 15:57
我觉得很好  给你点个赞

谢 大佬
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行