用 Python 做 9 件奇妙的事

2025-06-07

用 Python 做 9 件奇妙的事

哈喽,小伙伴们!我们都爱 Python,对吧?今天我们就来了解一下 Python 能实现的一些神奇功能(你可能还不知道)。有趣吗?那就开始吧!

1. 使用 Python 进行 Google 搜索

有时我们太沉迷于编程,以至于懒得打开浏览器搜索查询。但有了强大的 Python 库google,我们只需编写三行代码即可搜索查询,无需手动打开浏览器搜索。

安装方法如下:

pip install google
Enter fullscreen mode Exit fullscreen mode

代码,

#import library 
from googlesearch import search
#write your query
query = "best course for python"
# displaying 10 results from the search
for i in search(query, tld="co.in", num=10, stop=10, pause=2):
    print(i)
#you will notice the 10 search results(website links) in the output.
Enter fullscreen mode Exit fullscreen mode

2. 下载 Instagram 帖子和个人资料图片

我们经常在 Instagram 上看到一些精彩的帖子,并想将它们离线保存在设备上。但这款应用提供了在线保存功能,方便以后查看,而不是离线保存。这可以通过强大的 Python 库来实现instaloader

要安装库,

pip install instaloader
Enter fullscreen mode Exit fullscreen mode

代码,

#to download all the posts of a profile 
import instaloader
#creating object
d = instaloader.Instaloader()
#sepcifying the profile name
profile_Name = 'enter the instagram_handle'
#do profile_pic_only = True, to download the profile picture
d.download_profile(profile_Name, profile_pic_only = False)
#you will notice a folder of this profile's name, under which all the posts will get downloaded
Enter fullscreen mode Exit fullscreen mode

3. 从视频文件中提取音频

在某些情况下,我们拥有 mp4 文件,但只需要其中的音频。例如,制作一个视频时,需要用到另一个视频的音频。我们费尽心思想获取相同的音频文件,但最终失败了,只好选择另一个音乐文件。这个问题可以通过 Python 库解决moviepy,因为我们可以通过这个库从视频文件中提取音频。

要安装库,

pip install moviepy
Enter fullscreen mode Exit fullscreen mode

代码,

#import library 
import moviepy.editor as mp 
#specify the mp4 file here(mention the file path if it is in different directory)
clip = mp.VideoFileClip('video.mp4')
#specify the name for mp3 extracted
clip.audio.write_audiofile('Audio.mp3')
#you will notice mp3 file will be created at the specified location.
Enter fullscreen mode Exit fullscreen mode

4. URL缩短器

当你需要经常处理长 URL 时,处理它们是一项非常繁琐的任务。于是就有了 URL 缩短器(例如 bit.ly 和 tinyurl)的出现。这些服务可以将 URL 缩短到 50 个字符以下。我们可以借助 Python 库创建自己的 URL 缩短器pyshorteners

要安装库,

pip install pyshorteners
Enter fullscreen mode Exit fullscreen mode

代码,

#import library 
import pyshorteners
#creating object
s=pyshorteners.Shortener()
#type the url
url = "type the youtube link here"
#print the shortend url
print(s.tinyurl.short(url))
Enter fullscreen mode Exit fullscreen mode

5. 图像转 PDF 转换器

有时,我们会把笔记或文档当成照片来用,这样学习起来会很困难。我们可能会按错顺序,导致学习过程变得混乱和烦人。为了解决这个问题,一个想法是收集所有图片,然后将它们转换成 PDF 文件。这可以用 Python 库来实现img2pdf

要安装库,

pip install img2pdf
Enter fullscreen mode Exit fullscreen mode

代码,

#import libraries
import os
import img2pdf
#specify the name for pdf file
with open("converted.pdf", "wb") as f:
    #collect all the images in a single folder and specify its location
    f.write(img2pdf.convert([i for i in os.listdir(files\images) if i.endswith(".jpg")]))
Enter fullscreen mode Exit fullscreen mode

6.抄袭检测器

处理内容写作最重要的因素之一是抄袭。当文件打包在一起时,甚至无法手动检查。因此需要抄袭检测工具。我们也可以借助 Python 库创建自己的抄袭检测器difflib。它可以用于检查设备上两个或多个文件之间的相似性。

要安装库,

pip install difflib
Enter fullscreen mode Exit fullscreen mode

代码,

#import the required library
from difflib import SequenceMatcher
   #opening two text files
   with open('file_one.txt') as file_1, open('file_two.txt') as file_2: 
        #read the files in another variables
        file1_data = file_1.read() 
        file2_data = file_2.read() 
        #since we have taken two files for detecting plagiarism, we mention two them here
        similarity_ratio = SequenceMatcher(None,file1_data,file2_data).ratio() 
        #print the plagiarsim ratio
        print(similarity_ratio) 
Enter fullscreen mode Exit fullscreen mode

7.语言翻译器

我们生活在一个多语言的世界。因此,为了理解彼此的语言,我们需要一个语言翻译器,因为我们无法学习这么多语言。我们可以借助 Python 库创建自己的语言翻译器Translator

要安装库,

pip install translate
Enter fullscreen mode Exit fullscreen mode

代码,

#import the library 
from translate import Translator
#specifying the language 
translator = Translator(to_lang="Hindi")
#typing the message
translation = translator.translate('Hello!!! Welcome to my class')
#print the translated message
print(translation)
Enter fullscreen mode Exit fullscreen mode

8.二维码生成器

我们在日常生活中经常看到二维码(Quick Response Code)。一个非常简单的例子就是支付应用,它能节省用户大量的时间。我们也可以使用 Python 库为网站或个人资料创建专属的二维码。qrcode

要安装库,

pip install qrcode
Enter fullscreen mode Exit fullscreen mode

代码,

#import the library
import qrcode
#link to the website
input_data = "https://car-price-prediction-project.herokuapp.com/"
#Creating object
#version: defines size of image from integer(1 to 40), box_size = size of each box in pixels, border = thickness of the border.
qr = qrcode.QRCode(version=1,box_size=10,border=5)
#add_date :  pass the input text
qr.add_data(input_data)
#converting into image
qr.make(fit=True)
#specify the foreground and background color for the img 
img = qr.make_image(fill='black', back_color='white')
#store the image
img.save('qrcode_img.png')
Enter fullscreen mode Exit fullscreen mode

9.下载Youtube视频

我们都会在 YouTube 上看到一些有用的内容,无论是教育目的还是娱乐目的。这个平台不收费,可以免费观看种类繁多的视频,数量不限。唯一的问题是,我们想下载这些视频以备将来使用。这里有一个很棒的 Python 库pytube,它支持下载。

要安装库,

pip install pytube
Enter fullscreen mode Exit fullscreen mode

代码,

#import the library 
from pytube import YouTube
#ask user to type in the link 
link = input("Enter the link of youtube video:  ")
#creating an object
yt = YouTube(link)
#to get the highest resolution
ys = yt.streams.get_highest_resolution()
#show the message until downloading
print("Downloading...")
#specifying the location for this video 
ys.download("Downloads\python")
#show the message when download is completed
print("Download completed!!")
Enter fullscreen mode Exit fullscreen mode

或者,你可以尝试一下Siddharth Chandra这篇文章的方法

好了,朋友们,就到这里吧。感谢阅读,祝您编程愉快!

最初发表于 Hahshnode

文章来源:https://dev.to/unitybuddy/9-amazing-things-to-do-with-python-1ln5
PREV
成为算法专家必须了解的 JavaScript 字符串方法
NEXT
成功程序员的 5 个习惯