13个python自动化高级脚本

1、Python 进行速度测试

# pip install pyspeedtest
# pip install speedtest
# pip install speedtest-cli
#method 1
import speedtest
speedTest = speedtest.Speedtest() 
print(speedTest.get_best_server())
#Check download speed
print(speedTest.download())
#Check upload speed
print(speedTest.upload())
# Method 2
import pyspeedtest
st = pyspeedtest.SpeedTest()
st.ping()
st.download()
st.upload()

2、谷歌搜索

# pip install google
from googlesearch import search
query = "Medium.com"

for url in search(query):
    print(url)

 3、网络机器人

# pip install selenium
import time
from selenium import webdriver
from selenium.webdriver.common.keys 
import Keysbot = webdriver.Chrome("chromedriver.exe")
bot.get('http://www.google.com')
search = bot.find_element_by_name('q')
search.send_keys("@codedev101")
search.send_keys(Keys.RETURN)
time.sleep(5)
bot.quit()

4、获取歌曲歌词

# pip install lyricsgenius
import lyricsgenius
api_key = "xxxxxxxxxxxxxxxxxxxxx"
genius = lyricsgenius.Genius(api_key)
artist = genius.search_artist("Pop Smoke", 
max_songs=5,sort="title")
song = artist.song("100k On a Coupe")
print(song.lyrics)

 5、获取照片的Exif数据

# Get Exif of Photo
# Method 1
# pip install pillow
import PIL.Image
import PIL.ExifTags
img = PIL.Image.open("Img.jpg")
exif_data = 
{
    PIL.ExifTags.TAGS[i]: j
    for i, j in img._getexif().items()
    if i in PIL.ExifTags.TAGS
}
print(exif_data)
# Method 2
# pip install ExifRead
import exifread
filename = open(path_name, 'rb')
tags = exifread.process_file(filename)
print(tags)

6、提取图像中的 OCR 文本

# pip install pytesseract
import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

t=Image.open("img.png")
text = pytesseract.image_to_string(t, config='')
print(text)

7、将照片转换为Cartonize

# pip install opencv-python
import cv2

img = cv2.imread('img.jpg')
grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
grayimg  = cv2.medianBlur(grayimg, 5)

edges = cv2.Laplacian(grayimg , cv2.CV_8U, ksize=5)
r,mask =cv2.threshold(edges,100,255,cv2.THRESH_BINARY_INV)
img2 = cv2.bitwise_and(img, img, mask=mask)
img2 = cv2.medianBlur(img2, 5)

cv2.imwrite("cartooned.jpg", mask)

8、清空回收站

# pip install winshell
import winshell
try:
    winshell.recycle_bin().empty(confirm=False, /show_progress=False, sound=True)
    print("Recycle bin is emptied Now")
except:
    print("Recycle bin already empty")

9、Python 图像增强


# pip install pillow
from PIL import Image,ImageFilter
from PIL import ImageEnhance

im = Image.open('img.jpg')

# Choose your filter
# add Hastag at start if you don't want to any filter below
en = ImageEnhance.Color(im)
en = ImageEnhance.Contrast(im)
en = ImageEnhance.Brightness(im)
en = ImageEnhance.Sharpness(im)# result
en.enhance(1.5).show("enhanced")

10、获取 Window 版本

# Window Versionimport wmi
data = wmi.WMI()
for os_name in data.Win32_OperatingSystem():
  print(os_name.Caption)
# Microsoft Windows 11 Home

11、将 PDF 转换为图像

# PDF to Images
import fitz
pdf = 'sample_pdf.pdf'
doc = fitz.open(pdf)

for page in doc:
    pix = page.getPixmap(alpha=False)
    pix.writePNG('page-%i.png' % page.number)

12、转换:十六进制到 RGB

# Conversion: Hex to RGB
def Hex_to_Rgb(hex):
    h = hex.lstrip('#')
    return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
print(Hex_to_Rgb('#c96d9d'))  # (201, 109, 157)
print(Hex_to_Rgb('#fa0515')) # (250, 5, 21)

13、网站状态

# pip install requests
#method 1
import urllib.request
from urllib.request import Request, urlopenreq = Request('https://medium.com/@pythonians', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).getcode()
print(webpage)  # 200
# method 2
import requests
r = requests.get("https://medium.com/@pythonians")
print(r.status_code) # 200

你可能感兴趣的:(python,python,自动化,开发语言)