


This article introduces the implementation method of downloading images in python (super simple), which has certain reference value. Now I share it with you. Friends in need can refer to it.
The editor below will explain it to you. Here is an article on how to implement downloading images in python (super simple). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.
We sometimes need to find and download pictures on the Internet. When the number is relatively small, right-click to save, and the picture can be downloaded easily. However, some pictures cannot be downloaded. If you have set special settings, right-clicking does not display the save option, or you need to download a lot of pictures, this situation can be easily solved by writing a Python crawler code!
1. Page crawling
#coding=utf-8 import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html html = getHtml("https://tieba.baidu.com/p/5582243679") print html
The page data crawling process defines the getHtml() function, whose function is to pass a URL to getHtml() and ultimately download the entire page.
2. Page data filtering
import re import urllib def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) imglist = re.findall(imgre,html) return imglist html = getHtml("https://tieba.baidu.com/p/5582243679") print getImg(html)
In page data filtering, a new function getImg() is defined , the function of this function is to filter out image addresses in .jpg format.
3. Image download
#coding=utf-8 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.read() return html def getImg(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) imglist = re.findall(imgre,html) x = 0 for imgurl in imglist: urllib.urlretrieve(imgurl,'%s.jpg' % x) x+=1 html = getHtml("https://tieba.baidu.com/p/5582243679") print getImg(html)
Obtain all qualified image URLs through a for loop and use urllib. The urlretrieve() method downloads the remote data to the local and renames it!
The following is a supplement
as follows:
import urllib.request response = urllib.request.urlopen('http://www.jb51.net/g/500/600') cat_img = response.read() with open('cat_500_600.jpg','wb') as f: f.write(cat_img)
urlopen() brackets can be either a string or a request object. When the string is passed in, it will be converted into a request object, so the code
response = urllib.request.urlopen('http://www.jb51.net/g/500/600') can also be written as
req = urllib.request.Request(' http://www.jb51.net/g/500/600')
1. response = urllib.request.urlopen(req)
2. response also has geturl, info, getcode methods
Code with open('cat_500_600.jpg','wb') as f:
f.write(cat_img) is equivalent to
1. f = open('cat_500_600.jpg','wb')
2. try:
3. data = f.write(cat_img)
4. Finally:
5. f.close()
Related recommendations:
Python simply calculates the file MD5 value Method example
Python simple method to control the computer
##
The above is the detailed content of Implementation method of downloading images in Python (super simple). For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
