13個(gè)簡(jiǎn)便高效的Python腳本分享
每天我們都會(huì)面臨許多需要高級(jí)編碼的編程挑戰(zhàn)。你不能用簡(jiǎn)單的 Python 基本語(yǔ)法來(lái)解決這些問(wèn)題。在本文中,我將分享 13 個(gè)高級(jí) Python 腳本,它們可以成為你項(xiàng)目中的便捷工具。如果你目前還用不到這些腳本,你可以先添加收藏,以備留用。
好了,我們現(xiàn)在開(kāi)始吧。
1.使用 Python 進(jìn)行速度測(cè)試
這個(gè)高級(jí)腳本幫助你使用 Python 測(cè)試你的 Internet 速度。只需安裝速度測(cè)試模塊并運(yùn)行以下代碼。
# 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.制作網(wǎng)絡(luò)機(jī)器人
該腳本將幫助你使用 Python 自動(dòng)化網(wǎng)站。你可以構(gòu)建一個(gè)可控制任何網(wǎng)站的網(wǎng)絡(luò)機(jī)器人。查看下面的代碼,這個(gè)腳本在網(wǎng)絡(luò)抓取和網(wǎng)絡(luò)自動(dòng)化中很方便。
# 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.獲取歌曲歌詞
這個(gè)高級(jí)腳本將向你展示如何從任何歌曲中獲取歌詞。首先,你必須從 Lyricsgenius 網(wǎng)站獲得免費(fèi)的 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數(shù)據(jù)
使用 Python Pillow 模塊獲取任何照片的 Exif 數(shù)據(jù)。查看下面提到的代碼。我提供了兩種方法來(lái)提取照片的 Exif 數(shù)據(jù)。
# 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 是一種從數(shù)字和掃描文檔中識(shí)別文本的方法。許多開(kāi)發(fā)人員使用它來(lái)讀取手寫(xiě)數(shù)據(jù),下面的 Python 代碼可以將掃描的圖像轉(zhuǎn)換為 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.將照片轉(zhuǎn)換為Cartonize
這個(gè)簡(jiǎn)單的高級(jí)腳本會(huì)將你的照片轉(zhuǎn)換為 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.清空回收站
這個(gè)簡(jiǎn)單的腳本可以讓你用 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 圖像增強(qiáng)
使用 Python Pillow 庫(kù)增強(qiáng)你的照片以使其看起來(lái)更好。在下面的代碼中,我實(shí)現(xiàn)了四種方法來(lái)增強(qiáng)任何照片。
# 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 版本
這個(gè)簡(jiǎn)單的腳本將幫助你獲得當(dāng)前使用的完整窗口版本。
# Window Versionimport wmi data = wmi.WMI() for os_name in data.Win32_OperatingSystem(): print(os_name.Caption) # Microsoft Windows 11 Home
11.將 PDF 轉(zhuǎn)換為圖像
使用以下代碼將所有 Pdf 頁(yè)轉(zhuǎn)換為圖像。
# 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.轉(zhuǎn)換十六進(jìn)制到 RGB
該腳本將簡(jiǎn)單地將 Hex 轉(zhuǎn)換為 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.網(wǎng)站狀態(tài)
你可以使用 Python 檢查網(wǎng)站是否正常運(yùn)行。檢查以下代碼,顯示200 ,表示網(wǎng)站已啟動(dòng),如果顯示為 404 ,則表示網(wǎng)站已關(guān)閉。
# 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到此這篇關(guān)于13個(gè)簡(jiǎn)便高效的Python腳本分享的文章就介紹到這了,更多相關(guān)Python腳本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例
今天小編就為大家分享一篇pandas的相關(guān)系數(shù)與協(xié)方差實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python 哈希表實(shí)現(xiàn)簡(jiǎn)單python字典代碼實(shí)例
這篇文章主要介紹了python 哈希表實(shí)現(xiàn)簡(jiǎn)單python字典代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python requests.post()方法中data和json參數(shù)的使用方法
這篇文章主要介紹了Python requests.post()方法中data和json參數(shù)的使用方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
numpy數(shù)組之讀寫(xiě)文件的實(shí)現(xiàn)
本文主要介紹了numpy數(shù)組之讀寫(xiě)文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
利用Python腳本實(shí)現(xiàn)自動(dòng)刷網(wǎng)課
這篇文章主要介紹了利用Python腳本實(shí)現(xiàn)自動(dòng)刷網(wǎng)課,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Pandas實(shí)現(xiàn)數(shù)據(jù)類(lèi)型轉(zhuǎn)換的一些小技巧匯總
這篇文章主要給大家匯總介紹了關(guān)于Pandas實(shí)現(xiàn)數(shù)據(jù)類(lèi)型轉(zhuǎn)換的一些小技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
python線(xiàn)程優(yōu)先級(jí)隊(duì)列知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的一篇關(guān)于python線(xiàn)程優(yōu)先級(jí)隊(duì)列知識(shí)點(diǎn)總結(jié),有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02

