13个Python高级脚本助力项目运行

大家好,在工作时可能会面临许多需要高级编码的编程挑战,此时难以用Python基本语法来解决这些问题。本文将介绍13个高级Python脚本,它们可以成为项目运行中的便捷工具,可以收藏这些脚本以备留用。

1.Python速度测试

这个高级脚本使用Python帮助测试Internet速度,只需安装速度测试模块并运行以下代码:

# 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.在谷歌上搜索

可以从Google搜索引擎中提取重定向URL,安装以下模块并运行代码:

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

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

3.制作网络机器人

该脚本将帮助使用Python自动化网站,可以构建一个控制任何网站的网络机器人。运行下面的代码,脚本在网络抓取和网络自动化中很方便。

# 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.获取歌曲歌词

这个高级脚本将展示如何从任何歌曲中获取歌词,首先必须从Lyricsgenius网站获得免费的API密钥,然后运行以下代码:

# 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数据

使用Python Pillow模块获取任何照片的Exif数据,提供两种方法来提取照片的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文本

OCR是一种从数字和扫描文档中识别文本的方法,许多开发人员使用它来读取手写数据,下面的Python代码可以将扫描的图像转换为 OCR 文本格式。

注意:必须从 Github 下载 tesseract.exe。

# 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

这个高级脚本会将照片转换为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.清空回收站

这个脚本可以用Python清空回收站,查看下面的代码以了解如何操作:

# 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图像增强

使用Python Pillow库增强图像使其质量更高,下面的代码使用了四种方法实现图像增强。

# 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页转换为图像:

# 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

该脚本可将Hex转换为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.网站状态

可以使用Python检查网站是否正常运行,运行以下代码,显示200表示网站已启动,如果显示404则表示网站已关闭。

# 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
  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

python慕遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值