Home Backend Development Python Tutorial Explain the example code of writing Python crawler to capture gif images on Rampage comics

Explain the example code of writing Python crawler to capture gif images on Rampage comics

Mar 10, 2017 pm 01:53 PM

This article explains how to write an example code for Python crawler to capture gif images on Rampage comics. The example code is Python3, which uses the urllib module, request module and BeautifulSoup module. Friends in need can refer to this article

The crawler I want to introduce is to grab the interesting GIF pictures on the Rampage comics for offline viewing. The crawler was developed using python3.3, mainly using the urllib, request and BeautifulSoup modules.

The urllib module provides a high-level interface for obtaining data from the World Wide Web. When we use urlopen() to open a URL, it is equivalent to us using Python's built-in open() to open a file. But the difference is that the former receives a URL as a parameter, and there is no way to perform a seek operation on the open file stream (from a low-level perspective, because it is actually a socket, it is natural that there is no way to perform a seek operation), while the latter What is received is a local file name.

Python's BeautifulSoup module can help you parse HTML and XML
First of all, you usually write a web crawler, that is, crawl the html source code and other content of the web page, and then analyze and extract the corresponding content.
This kind of work of analyzing html content, if you just use the ordinary regular expression re module to match bit by bit, it is basically enough for analyzing web pages with simpler content.
But if you have to parse HTML that has a heavy workload and complicated content, you will find it impossible or difficult to implement using the re module.
If you use the beautifulsoup module to help you analyze html source code, you will find that things become so simple, which greatly improves the efficiency of analyzing html source code.
Note: BeautifulSoup is a third-party library, I use bs4. urllib2 is assigned to urllib.request in python3. The original text in the document is as follows.
Note: The urllib2 module has been split across several modules in Python 3 named urllib.requestand urllib.error.
The crawler source code is as follows

# -*- coding: utf-8 -*-

import urllib.request
import bs4,os

page_sum = 1 #设置下载页数

path = os.getcwd()
path = os.path.join(path,'暴走GIF')
if not os.path.exists(path):
  os.mkdir(path)                 #创建文件夹

url = "http://baozoumanhua.com/gif/year"   #url地址
headers = {                     #伪装浏览器
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'
         ' Chrome/32.0.1700.76 Safari/537.36'
}

for count in range(page_sum):
  req = urllib.request.Request(
    url = url+str(count+1),
    headers = headers
  )
  print(req.full_url)
  content = urllib.request.urlopen(req).read()

  soup = bs4.BeautifulSoup(content)          # BeautifulSoup
  img_content = soup.findAll('img',attrs={'style':'width:460px'})

  url_list = [img['src'] for img in img_content]   #列表推导 url
  title_list = [img['alt'] for img in img_content]  #图片名称

  for i in range(url_list.__len__()) :
    imgurl = url_list[i]
    filename = path + os.sep +title_list[i] + ".gif"
    print(filename+":"+imgurl)             #打印下载信息
    urllib.request.urlretrieve(imgurl,filename)    #下载图片
Copy after login

On line 15, you can modify the number of downloaded pages and save this file as baozougif.py. After running the command python baozougif.py, a folder of "Rampage GIF" will be generated in the same directory. All pictures will be automatically Download to this directory.

The above is the detailed content of Explain the example code of writing Python crawler to capture gif images on Rampage comics. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

How Much Python Can You Learn in 2 Hours? How Much Python Can You Learn in 2 Hours? Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 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.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles