Software Testing|Teach you how to use Python to download pictures

Insert image description here

Preface

I have always felt that the default desktop background of Windows system is not good-looking, but I don’t have good resources to replace it. Suddenly a friend of mine reminded me that there are so many pictures on the Internet, and you can even change many good-looking backgrounds every day, but If I were asked to set it up manually, I think it would be too troublesome. I might as well use technical means to download the pictures locally and use technical means to realize automatic replacement. However, the way is to go step by step. First implement Python to download pictures. This article will introduce how to use Python to download images.

Preliminary preparation

  1. Environmental preparation

First, we need to install Python's requestslibraries and BeautifulSouplibraries for sending HTTP requests and parsing HTML pages. The installation command is as follows:

pip install requests
pip install beautifulsoup4
  1. Analyze web page structure

Before crawling images from a specific website, we need to view the source code of the web page to understand the structure of the web page and the location of the images. We can obtain the web page source code through the browser's developer tools (F12) or using Python's requests library.

Crawl image links

  1. Send HTTP request and obtain web page source code
import requests

url = "http://www.example.com"
headers = {
    
    
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}

response = requests.get(url, headers=headers)
html = response.text
  1. Parse HTML pages
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, "html.parser")
  1. Get image link
image_links = []

# 根据网页结构和图片位置提取图片链接
for img_tag in soup.find_all("img"):
    image_links.append(img_tag["src"])

Download pictures

  1. Create a folder to save pictures
import os

# 创建保存图片的文件夹
if not os.path.exists("images"):
    os.makedirs("images")
  1. Download images and save to folder
for i, image_link in enumerate(image_links):
    response = requests.get(image_link, headers=headers)
    with open(f"images/image{
      
      i+1}.jpg", "wb") as file:
        file.write(response.content)

Complete code

import os
import requests
from bs4 import BeautifulSoup

url = "http://www.example.com"
headers = {
    
    
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}

# 发送HTTP请求并获取网页源代码
response = requests.get(url, headers=headers)
html = response.text

# 解析HTML页面
soup = BeautifulSoup(html, "html.parser")

# 获取图片链接
image_links = []
for img_tag in soup.find_all("img"):
    image_links.append(img_tag["src"])

# 创建保存图片的文件夹
if not os.path.exists("images"):
    os.makedirs("images")

# 下载图片并保存到文件夹
for i, image_link in enumerate(image_links):
    response = requests.get(image_link, headers=headers)
    with open(f"images/image{
      
      i+1}.jpg", "wb") as file:
        file.write(response.content)

Summarize

This article mainly introduces how to use Python to automatically download images from the website. I hope it can give you some help.

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132882096