current position:Home>Python cloud cat

Python cloud cat

2022-01-31 12:49:26 My cat's name is page

Suck cats with code ! This article is participating in 【 Meow star essay solicitation activity 】.

Nov-12-2021 18-12-53.gif

Here comes your cloud cat , coming coming It's all yours


Environmental Science

  • macOs Monterey 12.0.1
  • python 3.7.1
  • urllib3

1. install homebrew ( Installed, please ignore ), Execute the following command at the terminal

/usr/bin/ruby -e "$(curl -fsSL [https://raw.githubusercontent.com/Homebrew/install/master/install]"
 Copy code 

2. install python

brew install python
 Copy code 

3. see python edition

python3 --version
 Copy code 

4. install urllib3

pip install urllib3
 Copy code 
Urllib3 Is a powerful , Clarity of organization , be used for HTTP Client's Python library pypi.org/project/url…

5. desktop Desktop Create a name myCat Folder , Create a cat.py The file of

  • Copy the following code to your cat.py In file
  • placekitten.com/ This is a fantastic website , Just add http://placekitten.com/ wide / high You can quickly get a picture of a cat , Width and height can be customized
  • Folder path : /Users/xxx/Desktop/myCat. , name = '/Users/xxx/Desktop/myCat/' + str(i) + '.jpg' In this code xxx Replace with your computer name , such as name = '/Users/juejin/Desktop/myCat/' + str(i) + '.jpg'

Xnip2021-11-12_17-28-59.jpg

import urllib.request   # python 3.7
for i in range(500,1000):   
    url = 'http://placekitten.com/' + str(i) + '/500'    #  Width : str(i)  Height : 500  Parameters can be modified at will ,  Default 500 * 500  Start  
    response = urllib.request.urlopen(url)
    cat_img = response.read()
    name = '/Users//Desktop/myCat/' + str(i - 499) + '.jpg'
    with open (name,'wb') as f: #  Opening a file in binary format is only used for writing 
        f.write(cat_img) #  Write operation of file 
        print (' Download complete page %d A picture of a cat ' % (i - 499))

 Copy code 

6. function , To download 500 Zhang Lianshou , Wait a moment and you'll get a steady stream of cat photos

python cat.py 
 Copy code 

Add

with open() as f usage

wb:   Opening a file in binary format is only used for writing . Overwrite the file if it already exists . If the file does not exist , Create a new file .
wb+:  Open a file in binary format for reading and writing . Overwrite the file if it already exists . If the file does not exist , Create a new file .
 Copy code 
file.read([size])    Returns the file data as a string , Optional parameters size Control the number of bytes read 
file.readlines([size])    Returns a list of lines in the file ,size Parameters can be chosen 
file.write(str)    Write string to file 
file.writelines(strings)    Writes a string sequence to a file 
file.close()    Close file 
file.closed     Indicates that the file has been closed , Otherwise False
 Copy code 

copyright notice
author[My cat's name is page],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311249232745.html

Random recommended