search
HomeBackend DevelopmentPython TutorialUse python to make an automatic image downloader in 5 minutes

Use python to make an automatic image downloader in 5 minutes

Jun 23, 2017 pm 02:26 PM
pythonActual combatreptile


Python crawler practice - automatic image downloader

Basic steps for making a crawler

By the way, through this small example, you can master some basic steps about making a crawler.

Generally speaking, making a crawler requires the following steps:

  1. Analyze requirements (yes, requirement analysis is very important, don’t tell me your teacher didn’t teach you )

  2. Analyze the web page source code and cooperate with F12 (the web page source code is not as messy as F12, do you want to see me to death?)

  3. Prepared Regular expression or XPath expression (the artifact mentioned earlier)

  4. Formally write python crawler code

Effect

Run:

Well, let me enter keywords and let me think about it, what should I enter? It seems to be a bit exposed.

Enter

It seems that the download has started! Great! , I looked at the downloaded pictures, and wow, instantly I felt like I had added a lot more emoticons....

Okay, that’s pretty much it.

Requirement Analysis

"I want pictures, but I don’t want to search online"
"It’s best to download them automatically"
......

This is Requirements, okay, let's start analyzing the requirements. At least two functions must be implemented, one is to search for pictures, and the other is to automatically download.

First of all, when searching for pictures, the easiest thing to think of is the result of crawling Baidu pictures. Okay, then let’s go to Baidu pictures and take a look.

That’s basically it. , quite beautiful.

Let's try to search for something. I type a bad word and a series of search results come out. What does this mean...

Just find one Enter

#Okay, we have seen a lot of pictures, it would be great if we could climb down all the pictures here. We saw that there is keyword information in the URL

# We tried to change the keyword directly in the URL, did it jump?

In this way, you can search for pictures with specific keywords through this URL, so in theory, we can search for specific pictures without opening the web page. The next question is how to implement automatic downloading. In fact, using previous knowledge, we know that we can use request to get the URL of the image, then crawl it down and save it as a .jpg.

So this project should be completed.

Analyzing web pages

Okay, let’s start with the next step, analyzing the web page source code. Here I first switch back to the traditional page. Why do I do this? Because Baidu pictures currently use the waterfall flow mode, dynamically loading pictures, which is very troublesome to process. The traditional page turning interface is much better.

Another tip here is: if you can crawl the mobile version, don’t crawl the computer version, because the code of the mobile version is very clear and it is easy to get the required content.

Okay, I switched back to the traditional version, but it still has page numbers and is comfortable to read.

Let’s right-click and view the source code

What the hell is this, how can we see it clearly! !

At this time, it’s time to use F12, the developer tool! Let's go back to the previous page, press F12, and the toolbar below will come out. What we need to use is the thing in the upper left corner. One is mouse following, and the other is switching mobile versions, both of which are very useful to us. We use the first

here


然后选择你想看源代码的地方,就可以发现,下面的代码区自动定位到了这个位置,是不是很NB!

我们复制这个地址

然后到刚才的乱七八糟的源代码里搜索一下,发现它的位置了!(小样!我还找不到你!)但是这里我们又疑惑了,这个图片怎么有这么多地址,到底用哪个呢?我们可以看到有thumbURL,middleURL,hoverURL,objURL

通过分析可以知道,前面两个是缩小的版本,hover是鼠标移动过后显示的版本,objURL应该是我们需要的,不信可以打开这几个网址看看,发现obj那个最大最清晰。

好了,找到了图片位置,我们就开始分析它的代码。我看看是不是所有的objURL全是图片

貌似都是以.jpg格式结尾的,那应该跑不了了,我们可以看到搜索出61条,说明应该有61个图片

编写正则表达式

通过前面的学习,写出如下的一条正则表达式不难把?

pic_url = re.findall('"objURL":"(.*?)",',html,re.S)

编写爬虫代码

好了,正式开始编写爬虫代码了。这里我们就用了2个包,一个是正则,一个是requests包,之前也介绍过了,没看的回去看!

#-*- coding:utf-8 -*-import reimport requests

然后我们把刚才的网址粘过来,传入requests,然后把正则表达式写好

url = 'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1460997499750_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%B0%8F%E9%BB%84%E4%BA%BA'



html = requests.get(url).text
pic_url = re.findall('"objURL":"(.*?)",',html,re.S)

理论有很多图片,所以要循环,我们打印出结果来看看,然后用request获取网址,这里由于有些图片可能存在网址打不开的情况,加个5秒超时控制。

pic_url = re.findall('"objURL":"(.*?)",',html,re.S)
i = 0for each in pic_url:print eachtry:
        pic= requests.get(each, timeout=10)except requests.exceptions.ConnectionError:print '【错误】当前图片无法下载'continue

好了,再就是把网址保存下来,我们在事先在当前目录建立一个picture目录,把图片都放进去,命名的时候,用数字命名把

    string = 'pictures\\'+str(i) + '.jpg'
    fp = open(string,'wb')
    fp.write(pic.content)
    fp.close()
    i += 1

整个代码就是这样:

#-*- coding:utf-8 -*-
import re
import requests

url = 'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1460997499750_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%B0%8F%E9%BB%84%E4%BA%BA'html = requests.get(url).text
pic_url = re.findall('"objURL":"(.*?)",',html,re.S)i = 0for each in pic_url:
    print each
    try:
        pic= requests.get(each, timeout=10)
    except requests.exceptions.ConnectionError:
        print '【错误】当前图片无法下载'
        continue
    string = 'pictures\\'+str(i) + '.jpg'
    fp = open(string,'wb')
    fp.write(pic.content)
    fp.close()i += 1

我们运行一下,看效果(什么你说这是什么IDE感觉很炫!?赶紧去装Pycharm,Pycharm的配置和使用看这个文章!)!

好了我们下载了58个图片,咦刚才不是应该是61个吗?


我们看,运行中出现了有一些图片下载不了


我们还看到有图片没显示出来,打开网址看,发现确实没了。

所以,百度有些图片它缓存到了自己的机器上,所以你还能看见,但是实际连接已经失效

好了,现在自动下载问题解决了,那根据关键词搜索图片呢?只要改url就行了,我这里把代码写下来了

    word = raw_input("Input key word: ")
    url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&ct=201326592&v=flip'result = requests.get(url)

好了,享受你第一个图片下载爬虫吧!!当然不只能下载百度的图片拉,依葫芦画瓢,你现在应该做很多事情了,比如爬取头像,爬淘宝展示图,或是...美女图片,捂脸。一切都凭客官你的想象了,当然,作为爬虫的第一个实例,虽然纯用request已经能解决很多问题了,但是效率还是不够高,如果想要高效爬取大量数据,还是用scrapy吧

The above is the detailed content of Use python to make an automatic image downloader in 5 minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor