Python爬虫抓取网络照片

1年前 (2024-04-26)
本节编写一个快速下载照片的程序,通过百度图片下载您想要的前 60 张图片,并将其保存相应的目录。本节实战案例是上一节《Python Request库安装和使用》图片下载案例的延伸。

分析url规律

打开百度图片翻页版(点击访问),该翻页版网址要妥善保留。其 url 规律如下:

页:https://image.baidu.com/search/flip?tn=baiduimage&word=python&pn=0

第二页:https://image.baidu.com/search/flip?tn=baiduimage&word=python&pn=20

第三页:https://image.baidu.com/search/flip?tn=baiduimage&word=python&pn=40

第n页:https://image.baidu.com/search/flip?tn=baiduimage&word=python&pn=20*(n-1)

百度为了限制爬虫,将原来的翻页版变为了“瀑布流”浏览形式,也就是通过滚动滑轮自动加载图片,此种方式在一定程度上限制了爬虫程序。

写正则表达式

通过上一节可以得知每一张图片有一个源地址如下所示:

data-imgurl="图片源地址"

图片源地址,并检查网页源代码,使用 Ctrl+F 搜索该地址,如下图所示:

request模块使用

图1:检查网页结构(点击看高清图)


使用上述方式依次检查几张图片,您会发现每张图片源地址,有如下三种匹配结果:

"thumbURL":"https://ss2.bdstati网站站点" rel="nofollow" />

re_bds='"hoverURL":"(.*?)"'

编写程序代码

下面使用 Requests 库的相应方法和属性编写程序代码,最终实现一个快速下载照片的小程序。

# -*- coding:utf8 -*-

import requests

import re

from urllib import parse

import os

class BaiduImageSpider(object):

def __init__(self):

self.url = 'https://image.baidu.com/search/flip?tn=baiduimage&word={}'

self.headers = {'User-Agent':'Mozilla/4.0'}

# 获取图片

def get_image(self,url,word):

#使用 requests模块得到响应对象

res= requests.get(url,headers=self.headers)

# 更改编码格式

res.encoding="utf-8"

# 得到html网页

html=res.text

print(html)

#正则解析

pattern = r网站站点" rel="nofollow" /> 程序执行结果

图2:程序执行图


目录文件下载图如下所示:

python爬虫实战

图3:程序执行结果