I'm coming ~ update online on time every day!!
Interested babies are not quick to click and pay attention 🤷♀️🤷♀️🤷♀️
Recently, I found many interesting small projects when doing Python project development, and they all have less code, are easy to use, and are very friendly to novices. So today, let's share some fun projects in Python from one line of code to 30 lines of code with the little ones! No more nonsense ~ on the source code 👇👇
Thirty lines of code to crawl any Baidu picture
import requests import re import time url = "http://image. baidu. com/search/index? TN = Baidu image & word = Pikachu“ urls = requests.get(url) # Open link urltext = urls.text # Get link full text urlre = re.compile('"objURL":"(.*?)"', re.S) # Writing regular expressions urllist = urlre.findall(urltext) # Match by regular with open("1.txt", "w") as txt: # Write the matching link to the file for i in urllist: txt.write(i + "\n") i = 0 # Loop through the list and download the picture for urlimg in urllist: time.sleep(3) # Program sleep for three seconds img = requests.get(urlimg, timeout = 5).content # Open picture link in binary form if img: with open(str(i) + ".jpg", "wb") as imgs: # Create a new jpg file and write it in binary print("Downloading page%s Picture %s" % (str(i+1), urlimg)) imgs.write(img) #Write picture to i += 1 if i == 3: #In order to avoid unlimited download, set the download picture to 3 here break else: print("Download failed!") print("Download complete!")
The effects are as follows:
Twenty five line code picture to character picture
from PIL import Image IMG = 't01b2a945701805d7f1.jpg' #Set picture file WIDTH = 150 #Sets the width of the character drawing HEIGHT = 80 #Sets the height of the character drawing OUTPUT = 'output5.txt' #Set the text file for storing character drawings ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") #Sets the character set to display def get_char(r,g,b,alpha = 256): if alpha == 0: return ' ' length = len(ascii_char) gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b) unit = (255.0 + 1)/length return ascii_char[int(gray/unit)] if __name__ == '__main__': im = Image.open(IMG) im = im.resize((WIDTH,HEIGHT), Image.NEAREST) txt = "" for i in range(HEIGHT): for j in range(WIDTH): txt += get_char(*im.getpixel((j,i))) txt += '\n' print(txt) with open(OUTPUT,'w') as f: f.write(txt)
The effects are as follows:
Draw the sunflower in ten lines of code:
from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos()) < 1: break end_fill() done()
The effects are as follows:
Two lines of code to make exclusive dynamic QR code
from MyQR import myqr myqr.run(words='https://hao.360.com/',picture='Sources/gakki.gif',save_name='qr4.png',colorized=True)
The effects are as follows:
One line of code to achieve heart-shaped pattern
print('\n'.join([''.join([('lovelovelove'[(x-y)%12]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)]))
The effects are as follows:
end: that's all for sharing. I also want more private letter Xiaobian with complete source code ~ skirt
The most important thing in learning Python is attitude. We are bound to encounter many problems in the process of learning, and we may not be able to solve them if we want to break our head. This is normal. Don't rush to deny ourselves and doubt ourselves. If you have difficulties in learning at the beginning and want to find a python learning and communication environment, you can join us and get the learning materials: 696455390
Recommended articles
- 1 .Powerful and easy to use! Introduction to Playwright, a new generation of reptile weapon
- 2 .How to use the For loop and While loop in Python?
- 3 .One skill a day: why can't it pass?
- 4 .Design idea and analysis of the ninth question of watching snow · Zhong'an 2021 KCTF autumn competition
- 5 .Get started with Django REST framework
- 6 .Crawling urban bus stops with python
Let me comment
Comments posted (0)
Related sites
Subscribed
Popular articles
- 1.One skill a day: why can't it pass?
- 2.Design idea and analysis of the ninth question of watching snow · Zhong'an 2021 KCTF autumn competition
- 3.Why isn't Python the future programming language?
- 4.Python context manager
- 5.Python handles kindle label text