{"id":1906,"date":"2024-04-09T12:41:17","date_gmt":"2024-04-09T04:41:17","guid":{"rendered":"https:\/\/www.gyxwsjdxh.com\/?p=1906"},"modified":"2024-04-09T12:41:17","modified_gmt":"2024-04-09T04:41:17","slug":"10-%e4%b8%aa-python-%e8%84%9a%e6%9c%ac%e6%9d%a5%e8%87%aa%e5%8a%a8%e5%8c%96%e4%bd%a0%e7%9a%84%e6%97%a5%e5%b8%b8%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/www.gyxwsjdxh.com\/1906.html","title":{"rendered":"10 \u4e2a Python \u811a\u672c\u6765\u81ea\u52a8\u5316\u4f60\u7684\u65e5\u5e38\u4efb\u52a1"},"content":{"rendered":"\n

\u82f1\u6587\uff1ahttps:\/\/<\/span>python.plainenglish.io\/<\/span>10-python-scripts-to-automate-your-daily-task-de1496fdf64a<\/span><\/a>\n

Haider Imtiaz<\/p>\u7ffb\u8bd1\uff1a\u6768\u5c0f\u7231\uff0c\u4fb5\u5220

\u5728\u8fd9\u4e2a\u81ea\u52a8\u5316\u65f6\u4ee3\uff0c\u6211\u4eec\u6709\u5f88\u591a\u91cd\u590d\u65e0\u804a\u7684\u5de5\u4f5c\u8981\u505a\u3002 \u60f3\u60f3\u8fd9\u4e9b\u4f60\u4e0d\u518d\u9700\u8981\u4e00\u6b21\u53c8\u4e00\u6b21\u5730\u505a\u7684\u65e0\u804a\u7684\u4e8b\u60c5\uff0c\u8ba9\u5b83\u81ea\u52a8\u5316\uff0c\u8ba9\u4f60\u7684\u751f\u6d3b\u66f4\u8f7b\u677e\u3002 \u90a3\u4e48\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u5411\u60a8\u4ecb\u7ecd 10 \u4e2a Python \u81ea\u52a8\u5316\u811a\u672c\uff0c\u4ee5\u4f7f\u4f60\u7684\u5de5\u4f5c\u66f4\u52a0\u81ea\u52a8\u5316\uff0c\u751f\u6d3b\u66f4\u52a0\u8f7b\u677e\u3002 \u56e0\u6b64\uff0c\u6ca1\u6709\u66f4\u591a\u7684\u91cd\u590d\u4efb\u52a1\u5c06\u8fd9\u7bc7\u6587\u7ae0\u653e\u5728\u60a8\u7684\u5217\u8868\u4e2d\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\u3002<\/p>\n

01\u3001\u89e3\u6790\u548c\u63d0\u53d6 HTML<\/p>\n

\u6b64\u81ea\u52a8\u5316\u811a\u672c\u5c06\u5e2e\u52a9\u4f60\u4ece\u7f51\u9875 URL \u4e2d\u63d0\u53d6 HTML\uff0c\u7136\u540e\u8fd8\u4e3a\u4f60\u63d0\u4f9b\u53ef\u7528\u4e8e\u89e3\u6790 HTML \u4ee5\u83b7\u53d6\u6570\u636e\u7684\u529f\u80fd\u3002\u8fd9\u4e2a\u5f88\u68d2\u7684\u811a\u672c\u5bf9\u4e8e\u7f51\u7edc\u722c\u866b\u548c\u90a3\u4e9b\u60f3\u8981\u89e3\u6790 HTML \u4ee5\u83b7\u53d6\u91cd\u8981\u6570\u636e\u7684\u4eba\u6765\u8bf4\u662f\u4e00\u79cd\u5f88\u597d\u7684\u4eab\u53d7\u3002<\/p>\n

# Parse and Extract HTML\n # pip install gazpacho\n\n import gazpacho\n\n # Extract HTML from URL\n url = https:\/\/www.example.com\/\n html = gazpacho.get(url)\n print(html)\n\n # Extract HTML with Headers\n headers = {User-Agent: Mozilla\/5.0}\n html = gazpacho.get(url, headers=headers)\n print(html)\n\n # Parse HTML\n parse = gazpacho.Soup(html)\n\n # Find single tags\n tag1 = parse.find(h1)\n tag2 = parse.find(span)\n\n # Find multiple tags\n tags1 = parse.find_all(p)\n tags2 = parse.find_all(a)\n\n # Find tags by class\n tag = parse.find(.class)\n\n # Find tags by Attribute\n tag = parse.find(“div”, attrs={“class”: “test”})\n\n # Extract text from tags\n text = parse.find(h1).text\n text = parse.find_all(p)[0].text\n <\/div>\n

02\u3001\u4e8c\u7ef4\u7801\u626b\u63cf\u4eea<\/p>\n

\u62e5\u6709\u5927\u91cf\u4e8c\u7ef4\u7801\u56fe\u50cf\u6216\u53ea\u60f3\u626b\u63cf\u4e8c\u7ef4\u7801\u56fe\u50cf\uff0c\u90a3\u4e48\u6b64\u81ea\u52a8\u5316\u811a\u672c\u5c06\u5e2e\u52a9\u4f60\u3002\u8be5\u811a\u672c\u4f7f\u7528 Qrtools \u6a21\u5757\uff0c\u4f7f\u4f60\u80fd\u591f\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u626b\u63cf QR \u56fe\u50cf\u3002<\/p>\n

# Qrcode Scanner\n # pip install qrtools\n\n from qrtools import Qr\n def Scan_Qr(qr_img):\n qr = Qr()\n qr.decode(qr_img)\n print(qr.data)\n return qr.data\n\n print(“Your Qr Code is: “, Scan_Qr(“qr.png”))\n <\/div>\n

03\u3001\u622a\u56fe<\/p>\n

\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e2a\u5f88\u68d2\u7684\u811a\u672c\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u622a\u53d6\u5c4f\u5e55\u622a\u56fe\u3002\u4f7f\u7528\u6b64\u811a\u672c\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u622a\u5c4f\u6216\u622a\u53d6\u7279\u5b9a\u533a\u57df\u7684\u5c4f\u5e55\u622a\u56fe\u3002<\/p>\n

# Grab Screenshot\n # pip install pyautogui\n # pip install Pillow\n\n from pyautogui import screenshot\n import time\n from PIL import ImageGrab\n\n # Grab Screenshot of Screen\n def grab_screenshot():\n shot = screenshot()\n shot.save(my_screenshot.png)\n\n # Grab Screenshot of Specific Area\n def grab_screenshot_area():\n area = (0, 0, 500, 500)\n shot = ImageGrab.grab(area)\n shot.save(my_screenshot_area.png)\n\n # Grab Screenshot with Delay\n def grab_screenshot_delay():\n time.sleep(5)\n shot = screenshot()\n shot.save(my_screenshot_delay.png)\n <\/div>\n

04\u3001\u521b\u5efa\u6709\u58f0\u8bfb\u7269<\/p>\n

\u538c\u5026\u4e86\u624b\u52a8\u5c06\u60a8\u7684 PDF \u4e66\u7c4d\u8f6c\u6362\u4e3a\u6709\u58f0\u8bfb\u7269\uff0c\u90a3\u4e48\u8fd9\u662f\u4f60\u7684\u81ea\u52a8\u5316\u811a\u672c\uff0c\u5b83\u4f7f\u7528 GTTS \u6a21\u5757\u5c06\u4f60\u7684 PDF \u6587\u672c\u8f6c\u6362\u4e3a\u97f3\u9891\u3002<\/p>\n

# Create Audiobooks\n # pip install gTTS\n # pip install PyPDF2\n\n from PyPDF2 import PdfFileReader as reader\n from gtts import gTTS\n\n def create_audio(pdf_file):\n read_Pdf = reader(open(pdf_file, rb))\n for page in range(read_Pdf.numPages):\n text = read_Pdf.getPage(page).extractText()\n tts = gTTS(text, lang=en)\n tts.save(page + str(page) + .mp3)\n\n create_audio(book.pdf)\n <\/div>\n

05\u3001PDF \u7f16\u8f91\u5668<\/p>\n

\u4f7f\u7528\u4ee5\u4e0b\u81ea\u52a8\u5316\u811a\u672c\u4f7f\u7528 Python \u7f16\u8f91 PDF \u6587\u4ef6\u3002\u8be5\u811a\u672c\u4f7f\u7528 PyPDF4 \u6a21\u5757\uff0c\u5b83\u662f PyPDF2 \u7684\u5347\u7ea7\u7248\u672c\uff0c\u4e0b\u9762\u6211\u7f16\u5199\u4e86 Parse Text\u3001Remove pages \u7b49\u5e38\u7528\u529f\u80fd\u3002\u5f53\u4f60\u6709\u5927\u91cf PDF \u6587\u4ef6\u8981\u7f16\u8f91\u6216\u9700\u8981\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5728 Python \u9879\u76ee\u4e2d\u4f7f\u7528\u811a\u672c\u65f6\uff0c\u8fd9\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u811a\u672c\u3002<\/p>\n

# PDF Editor\n # pip install PyPDf4\n\n import PyPDF4\n\n # Parse the Text from PDF\n def parse_text(pdf_file):\n reader = PyPDF4.PdfFileReader(pdf_file)\n for page in reader.pages:\n print(page.extractText())\n\n # Remove Page from PDF\n def remove_page(pdf_file, page_numbers):\n filer = PyPDF4.PdfReader(source.pdf, rb)\n out = PyPDF4.PdfWriter()\n for index in page_numbers:\n page = filer.pages[index]\n out.add_page(page)\n with open(rm.pdf, wb) as f:\n out.write(f)\n\n # Add Blank Page to PDF\n def add_page(pdf_file, page_number):\n reader = PyPDF4.PdfFileReader(pdf_file)\n writer = PyPDF4.PdfWriter()\n writer.addPage()\n with open(add.pdf, wb) as f:\n writer.write(f)\n\n # Rotate Pages\n def rotate_page(pdf_file):\n reader = PyPDF4.PdfFileReader(pdf_file)\n writer = PyPDF4.PdfWriter()\n for page in reader.pages:\n page.rotateClockwise(90)\n writer.addPage(page)\n with open(rotate.pdf, wb) as f:\n writer.write(f)\n\n # Merge PDFs\n def merge_pdfs(pdf_file1, pdf_file2):\n pdf1 = PyPDF4.PdfFileReader(pdf_file1)\n pdf2 = PyPDF4.PdfFileReader(pdf_file2)\n writer = PyPDF4.PdfWriter()\n for page in pdf1.pages:\n writer.addPage(page)\n for page in pdf2.pages:\n writer.addPage(page)\n with open(merge.pdf, wb) as f:\n writer.write(f)\n <\/div>\n

06\u3001\u8ff7\u4f60 Stackoverflow<\/p>\n

\u4f5c\u4e3a\u4e00\u540d\u7a0b\u5e8f\u5458\uff0c\u6211\u77e5\u9053\u6211\u4eec\u6bcf\u5929\u90fd\u9700\u8981 StackOverflow\uff0c\u4f46\u4f60\u4e0d\u518d\u9700\u8981\u5728 Google \u4e0a\u641c\u7d22\u5b83\u3002\u73b0\u5728\uff0c\u5728\u60a8\u7ee7\u7eed\u5904\u7406\u9879\u76ee\u7684\u540c\u65f6\uff0c\u5728\u4f60\u7684 CMD \u4e2d\u83b7\u5f97\u76f4\u63a5\u89e3\u51b3\u65b9\u6848\u3002\u901a\u8fc7\u4f7f\u7528 Howdoi \u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u83b7\u5f97 StackOverflow \u89e3\u51b3\u65b9\u6848\u3002\u4f60\u53ef\u4ee5\u5728\u4e0b\u9762\u627e\u5230\u4e00\u4e9b\u53ef\u4ee5\u5c1d\u8bd5\u7684\u793a\u4f8b\u3002<\/p>\n

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

07\u3001\u81ea\u52a8\u5316\u624b\u673a<\/p>\n

\u6b64\u81ea\u52a8\u5316\u811a\u672c\u5c06\u5e2e\u52a9\u4f60\u4f7f\u7528 Python \u4e2d\u7684 Android \u8c03\u8bd5\u6865 (ADB) \u81ea\u52a8\u5316\u4f60\u7684\u667a\u80fd\u624b\u673a\u3002\u4e0b\u9762\u6211\u5c06\u5c55\u793a\u5982\u4f55\u81ea\u52a8\u6267\u884c\u5e38\u89c1\u4efb\u52a1\uff0c\u4f8b\u5982\u6ed1\u52a8\u624b\u52bf\u3001\u547c\u53eb\u3001\u53d1\u9001\u77ed\u4fe1\u7b49\u7b49\u3002\u60a8\u53ef\u4ee5\u4e86\u89e3\u6709\u5173 ADB \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u5e76\u63a2\u7d22\u66f4\u591a\u4ee4\u4eba\u5174\u594b\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u624b\u673a\u81ea\u52a8\u5316\uff0c\u8ba9\u60a8\u7684\u751f\u6d3b\u66f4\u8f7b\u677e\u3002<\/p>\n

# Automate Mobile Phones\n # pip install opencv-python\n\n import subprocess\n def main_adb(cm):\n p = subprocess.Popen(cm.split( ), stdout=subprocess.PIPE, shell=True)\n (output, _) = p.communicate()\n return output.decode(utf-8)\n\n # Swipe\n def swipe(x1, y1, x2, y2, duration):\n cmd = adb shell input swipe {} {} {} {} {}.format(x1, y1, x2, y2, duration)\n return main_adb(cmd)\n\n # Tap or Clicking\n def tap(x, y):\n cmd = adb shell input tap {} {}.format(x, y)\n return main_adb(cmd)\n\n # Make a Call\n def make_call(number):\n cmd = f”adb shell am start -a android.intent.action.CALL -d tel:{number}”\n return main_adb(cmd)\n\n # Send SMS\n def send_sms(number, message):\n cmd = adb shell am start -a android.intent.action.SENDTO -d sms:{} –es sms_body “{}”.format(number, message)\n return main_adb(cmd)\n\n # Download File From Mobile to PC\n def download_file(file_name):\n cmd = adb pull \/sdcard\/{}.format(file_name)\n return main_adb(cmd)\n\n # Take a screenshot\n def screenshot():\n cmd = adb shell screencap -p\n return main_adb(cmd)\n\n # Power On and Off\n def power_off():\n cmd = “adb shell input keyevent 26”\n return main_adb(cmd)\n <\/div>\n

08\u3001\u76d1\u63a7 CPU\/GPU \u6e29\u5ea6<\/p>\n

\u4f60\u53ef\u80fd\u4f7f\u7528 CPU-Z \u6216\u4efb\u4f55\u89c4\u683c\u76d1\u63a7\u8f6f\u4ef6\u6765\u6355\u83b7\u4f60\u7684 Cpu \u548c Gpu \u6e29\u5ea6\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u8fdb\u884c\u3002\u597d\u5427\uff0c\u8fd9\u4e2a\u811a\u672c\u4f7f\u7528 Pythonnet \u548c OpenhardwareMonitor \u6765\u5e2e\u52a9\u4f60\u76d1\u63a7\u5f53\u524d\u7684 Cpu \u548c Gpu \u6e29\u5ea6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u5728\u8fbe\u5230\u4e00\u5b9a\u6e29\u5ea6\u65f6\u901a\u77e5\u81ea\u5df1\uff0c\u4e5f\u53ef\u4ee5\u5728 Python \u9879\u76ee\u4e2d\u4f7f\u7528\u5b83\u6765\u7b80\u5316\u65e5\u5e38\u751f\u6d3b\u3002<\/p>\n

# Get CPU\/GPU Temperature\n # pip install pythonnet\n\n import clr\n clr.AddReference(“OpenHardwareMonitorLib”)\n from OpenHardwareMonitorLib import *\n\n spec = Computer()\n spec.GPUEnabled = True\n spec.CPUEnabled = True\n spec.Open()\n\n # Get CPU Temp\n def Cpu_Temp():\n while True:\n for cpu in range(0, len(spec.Hardware[0].Sensors)):\n if “\/temperature” in str(spec.Hardware[0].Sensors[cpu].Identifier):\n print(str(spec.Hardware[0].Sensors[cpu].Value))\n\n # Get GPU Temp\n def Gpu_Temp()\n while True:\n for gpu in range(0, len(spec.Hardware[0].Sensors)):\n if “\/temperature” in str(spec.Hardware[0].Sensors[gpu].Identifier):\n print(str(spec.Hardware[0].Sensors[gpu].Value))\n <\/div>\n

09\u3001Instagram \u4e0a\u4f20\u673a\u5668\u4eba<\/p>\n

Instagram \u662f\u4e00\u4e2a\u8457\u540d\u7684\u793e\u4ea4\u5a92\u4f53\u5e73\u53f0\uff0c\u4f60\u73b0\u5728\u4e0d\u9700\u8981\u901a\u8fc7\u667a\u80fd\u624b\u673a\u4e0a\u4f20\u7167\u7247\u6216\u89c6\u9891\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u811a\u672c\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u6267\u884c\u6b64\u64cd\u4f5c\u3002<\/p>\n

# Upload Photos and Video on Insta\n # pip install instabot\n\n from instabot import Bot\n\n def Upload_Photo(img):\n robot = Bot()\n robot.login(username=”user”, password=”pass”)\n robot.upload_photo(img, caption=”Medium Article”)\n print(“Photo Uploaded”)\n\n def Upload_Video(video):\n robot = Bot()\n robot.login(username=”user”, password=”pass”)\n robot.upload_video(video, caption=”Medium Article”)\n print(“Video Uploaded”)\n\n def Upload_Story(img):\n robot = Bot()\n robot.login(username=”user”, password=”pass”)\n robot.upload_story(img, caption=”Medium Article”)\n print(“Story Photos Uploaded”)\n\n Upload_Photo(“img.jpg”)\n Upload_Video(“video.mp4”)\n <\/div>\n

10\u3001\u89c6\u9891\u6c34\u5370<\/p>\n

\u4f7f\u7528\u6b64\u81ea\u52a8\u5316\u811a\u672c\u4e3a\u4f60\u7684\u89c6\u9891\u6dfb\u52a0\u6c34\u5370\uff0c\u8be5\u811a\u672c\u4f7f\u7528 Moviepy\uff0c\u8fd9\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u89c6\u9891\u7f16\u8f91\u6a21\u5757\u3002\u5728\u4e0b\u9762\u7684\u811a\u672c\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u6dfb\u52a0\u6c34\u5370\u5e76\u4e14\u53ef\u4ee5\u81ea\u7531\u4f7f\u7528\u5b83\u3002<\/p>\n

# Video Watermark with Python\n # pip install moviepy\n\n from moviepy.editor import *\n clip = VideoFileClip(“myvideo.mp4”, audio=True)\n width,height = clip.size\n text = TextClip(“WaterMark”, font=Arial, color=white, fontsize=28)\n\n set_color = text.on_color(size=(clip.w + text.w, text.h-10), color=(0,0,0), pos=(6,center), col_opacity=0.6)\n set_textPos = set_color.set_pos( lambda pos: (max(width\/30,int(width-0.5* width* pos)),max(5*height\/6,int(100* pos))) )\n\n Output = CompositeVideoClip([clip, set_textPos])\n Output.duration = clip.duration\n Output.write_videofile(“output.mp4”, fps=30, codec=libx264)\n <\/div>\n

— EOF —<\/p>\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"

\u82f1\u6587\uff1ahttps:\/\/python.plainenglish.io\/10-python-scripts-to-automate-your-d… <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1906","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/posts\/1906"}],"collection":[{"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/comments?post=1906"}],"version-history":[{"count":1,"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/posts\/1906\/revisions"}],"predecessor-version":[{"id":1907,"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/posts\/1906\/revisions\/1907"}],"wp:attachment":[{"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/media?parent=1906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/categories?post=1906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gyxwsjdxh.com\/wp-json\/wp\/v2\/tags?post=1906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}