作為程序員,工作中經(jīng)常要應(yīng)對(duì)編碼方面的挑戰(zhàn),而這些挑戰(zhàn)所需要的復(fù)雜解決方案超出了基本的Python/ target=_blank class=infotextkey>Python語(yǔ)法。從StackOverflow上復(fù)制粘貼片段只能幫助解決一部分問(wèn)題。
為了提高你的Python技能,你需要一個(gè)能自動(dòng)執(zhí)行繁瑣任務(wù)的高級(jí)腳本工具箱。
在本文中,將分享8個(gè)有趣又實(shí)用的Python腳本,它們將自動(dòng)完成繁瑣的工作。這些小腳本將為編程項(xiàng)目增添一些樂(lè)趣,并幫助你更聰明地工作。
1.用Python測(cè)試網(wǎng)速
這個(gè)有趣的腳本可以幫助用戶(hù)使用Python測(cè)試網(wǎng)速。只需安裝speedtest模塊并運(yùn)行代碼即可。
# pip install pyspeedtest
# pip install speedtest
# pip install speedtest-cli
# 方法1
import speedtest
speedTest = speedtest.Speedtest()
print(speedTest.get_best_server())
# 檢查下載速度
print(speedTest.download())
# 檢查上傳速度
print(speedTest.upload())
# 方法2
import pyspeedtest
st = pyspeedtest.SpeedTest()
st.ping()
st.download()
st.upload()
2.將照片轉(zhuǎn)換為卡通格式
這個(gè)簡(jiǎn)單的腳本可將照片轉(zhuǎn)換為卡通格式,非常有趣。
# 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("cartoonize.jpg", mask)
3.網(wǎng)站狀態(tài)
可以使用Python來(lái)檢查網(wǎng)站是否正常運(yùn)行。
# pip install requests
# 方法 1
import urllib.request
from urllib.request import Request, urlopenreq = Request('https://medium.com/@vesper7', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).getcode()
print(webpage) # 200
# 方法 2
import requests
r = requests.get("https://medium.com/@vesper7")
print(r.status_code) # 200
4.Python圖像增強(qiáng)
使用Python Pillow庫(kù)增強(qiáng)照片效果。本文實(shí)現(xiàn)了如下四種增強(qiáng)照片效果的方法。
# pip install pillow
from PIL import Image,ImageFilter
from PIL import ImageEnhance
im = Image.open('img.jpg')
# 選擇你的濾鏡
# 如果不想使用下面的任何濾鏡,請(qǐng)?jiān)陂_(kāi)始時(shí)添加Hastag
en = ImageEnhance.Color(im)
en = ImageEnhance.Contrast(im)
en = ImageEnhance.Brightness(im)
en = ImageEnhance.Sharpness(im)# result
en.enhance(1.5).show("enhanced")
5.制作Web機(jī)器人
該腳本可幫助你用Python實(shí)現(xiàn)網(wǎng)站自動(dòng)化。你可以創(chuàng)建一個(gè)網(wǎng)絡(luò)機(jī)器人,控制任何網(wǎ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()
6.轉(zhuǎn)換:十六進(jìn)制轉(zhuǎn)換為RGB
此腳本可簡(jiǎn)單地將十六進(jìn)制轉(zhuǎn)換為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)
7.將PDF轉(zhuǎn)換為圖像
使用這段代碼將所有PDF頁(yè)面轉(zhuǎn)換為圖像。
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)
8.獲取歌曲歌詞
這個(gè)有趣的腳本展示了如何從任何歌曲中提取歌詞。首先,需要從Lyricsgenius網(wǎng)站獲取一個(gè)免費(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)
這8個(gè)有趣而實(shí)用的Python腳本將自動(dòng)執(zhí)行枯燥任務(wù),為繁雜的工作帶來(lái)快樂(lè)和效率。