Home Backend Development Python Tutorial python爬虫入门教程之糗百图片爬虫代码分享

python爬虫入门教程之糗百图片爬虫代码分享

Jun 06, 2016 am 11:32 AM
python

学习python少不了写爬虫,不仅能以点带面地学习、练习使用python,爬虫本身也是有用且有趣的,大量重复性的下载、统计工作完全可以写一个爬虫程序完成。

用python写爬虫需要python的基础知识、涉及网络的几个模块、正则表达式、文件操作等知识。昨天在网上学习了一下,写了一个爬虫自动下载「糗事百科」里面的图片。源代码如下:

代码如下:


# -*- coding: utf-8 -*-
# 上面那句让代码里支持中文

#--------------------------------------- 
#   程序:糗百图片爬虫 
#   版本:0.1 
#   作者:赵伟 
#   日期:2013-07-25 
#   语言:Python 2.7 
#   说明:能设置下载的页数。没有做更多抽象和交互方面的优化。 
#---------------------------------------

import urllib2
import urllib
import re

#正则表达式,用来抓取图片的地址
pat = re.compile('

\\npython爬虫入门教程之糗百图片爬虫代码分享')

#用来合成网页的URL
nexturl1 = "http://m.qiushibaike.com/imgrank/page/"
nexturl2 = "?s=4582487&slow"

#页数计数
count = 1

#设置抓取的页数
while count

    print "Page " + str(count) + "\n"
    myurl = nexturl1 + str(count) + nexturl2
    myres = urllib2.urlopen(myurl)#抓取网页
    mypage = myres.read()#读取网页内容
    ucpage = mypage.decode("utf-8") #转码

    mat = pat.findall(ucpage)#用正则表达式抓取图片地址
       
    count += 1;
   
    if len(mat):
        for item in mat:
            print "url: " + item + "\n"
            fnp = re.compile('/(\w+\.\w+)$')#下面三行分离出图片文件的名称
            fnr = fnp.findall(item)
            fname = fnr[0]
            urllib.urlretrieve(item, fname)#下载图片
      
    else:
        print "no data"

使用方法:新建一个practice文件夹,将源代码保存为qb.py文件,并放在practice文件夹中,在命令行里执行python qb.py,即开始下载图片。可以修改源代码里面的while语句设置下载的页数。

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 Article Tags

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)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles