没想到奥特曼竟然还真的有这么多种类 。

Python 爬虫 120 例,已完成文章清单

  1. 10 行代码集 2000 张美女图 ,Python 爬虫 120 例,再上征途
  2. 通过 Python 爬虫,发现 60%女装大佬游走在 cosplay 领域
  3. Python 千猫图 ,简单技术满足你的收集控

本篇博客目标

爬取目标

使用框架

  • requests,re

重点学习的内容

  • get 请求;
  • requests 请求超时设定,timeout 参数;
  • re 模块正则表达式;
  • 数据去重;
  • URL 地址拼接。

列表页分析
经过开发者工具的简单查阅 ,得到全部奥特曼卡片所在的 DOM 标签为 <li class="item"></li>
详情页所在的标签为 <a = href="详情页" ……

具体标签所在元素如下图所示:

稍后根据实际请求数据,整理一下正则表达式。

详情页

点击任意目标数据,进入详情页 ,详情页获取奥特曼图片,图片地址所在位置如下图所示 。


右键可获得图片的所在标签 。

整理需求如下

  1. 通过列表页,爬取全部奥特曼详情页的地址;
  2. 进入详情页 ,爬取详情页里面的图片地址;
  3. 下载保存图片;

代码实现

爬取全部奥特曼详情页地址

在爬取列表页的过程中,发现奥特曼页面使用了 iframe 嵌套,该手段也属于最简单的反爬手段 ,提取真实链接即可,故目标数据源切换为 http://www.ultramanclub.com/allultraman/ 。

import requests
import re
import time


# 爬虫入口
def run():
    url = "http://www.ultramanclub.com/allultraman/"
    try:
        # 网页访问速度慢,需要设置 timeout
        res = requests.get(url=url, timeout=10)
        res.encoding = "gb2312"
        html = res.text
        get_detail_list(html)

    except Exception as e:
        print("请求异常", e)


# 获取全部奥特曼详情页
def get_detail_list(html):
    start_index = '<ul class="lists">'
    start = html.find(start_index)
    html = html[start:]
    links = re.findall('<li class="item"><a href="(.*)">', html)
    print(len(links))
    links = list(set(links))
    print(len(links))


if __name__ == '__main__':
    run()

在代码编写过程中 ,发现网页访问速度慢 ,故设置 timeout 属性为 10,防止出现异常,

正则表达式匹配数据时 ,出现了重复数据,通过 set 集合进行去重,最终在转换为 list。

接下来对获取到的 list 进行二次拼接 ,获取详情页地址 。

进行二次拼接得到的详情页地址,代码如下:

# 获取全部奥特曼详情页
def get_detail_list(html):
    start_index = '<ul class="lists">'
    start = html.find(start_index)
    html = html[start:]
    links = re.findall('<li class="item"><a href="(.*)">', html)
    # links = list(set(links))
    links = [f"http://www.ultramanclub.com/allultraman/{i.split('/')[1]}/" for i in set(links)]
    print(links)

爬取全部奥特曼大图

该步骤先获取网页标题的方式,然后用该标题 ,对奥特曼大图爬取命名。

爬取逻辑非常简单,只需要循环上文爬取到详情页地址,然后通过正则表达式进行匹配即可。

修改代码如下所示 ,关键节点查看注释 。

import requests
import re
import time

# 声明 UA
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
}
# 存储异常路径,防止出现爬取失败情况
error_list = []

# 爬虫入口
def run():
    url = "http://www.ultramanclub.com/allultraman/"
    try:
        # 网页访问速度慢,需要设置 timeout
        res = requests.get(url=url, headers=headers, timeout=10)
        res.encoding = "gb2312"
        html = res.text
        return get_detail_list(html)

    except Exception as e:
        print("请求异常", e)


# 获取全部奥特曼详情页
def get_detail_list(html):
    start_index = '<ul class="lists">'
    start = html.find(start_index)
    html = html[start:]
    links = re.findall('<li class="item"><a href="(.*)">', html)
    # links = list(set(links))
    links = [
        f"http://www.ultramanclub.com/allultraman/{i.split('/')[1]}/" for i in set(links)]
    return links


def get_image(url):
    try:
        # 网页访问速度慢	,需要设置 timeout
        res = requests.get(url=url, headers=headers, timeout=15)
        res.encoding = "gb2312"
        html = res.text
        print(url)
        # 获取详情页标题,作为图片文件名
        title = re.search('<title>(.*?)\[', html).group(1)
        # 获取图片短连接地址
        image_short = re.search(
            '<figure class="image tile">[.\s]*?<img src="(.*?)"', html).group(1)

        # 拼接完整图片地址
        img_url = "http://www.ultramanclub.com/allultraman/" + image_short[3:]
        # 获取图片数据
        img_data = requests.get(img_url).content
        print(f"正在爬取{title}")
        if title is not None and image_short is not None:
            with open(f"images/{title}.png", "wb") as f:
                f.write(img_data)

    except Exception as e:
        print("*"*100)
        print(url)
        print("请求异常", e)

        error_list.append(url)


if __name__ == '__main__':
    details = run()
    for detail in details:
        get_image(detail)

    while len(error_list) > 0:
        print("再次爬取")
        detail = error_list.pop()
        get_image(detail)

    print("奥特曼图片数据爬取完毕")

运行代码,看到图片接连存储到本地 images 目录中。


代码说明:

上述代码在主函数中,对列表页抓取到的详情页进行了循环抓取。即如下部分代码:

 for detail in details:
        get_image(detail)

由于本网站爬取速度慢 ,故在 get_image 函数中的 get 请求里面,加入了 timeout=15 的设定 。

图片地址正则匹配与地址拼接,使用的代码如下:

# 获取详情页标题	,作为图片文件名
title = re.search('<title>(.*?)\[', html).group(1)
# 获取图片短连接地址
image_short = re.search(
    '<figure class="image tile">[.\s]*?<img src="(.*?)"', html).group(1)

# 拼接完整图片地址
img_url = "http://www.ultramanclub.com/allultraman/" + image_short[3:]

哎,这些奥特曼果然长得不一样。

完整代码下载地址:https://codechina.csdn.net/hihell/python120

如果你不想运行代码,只想要图 ,购买一份吧:https://download.csdn.net/download/hihell/19543243

抽奖时间(目前累计送出 4 份)

很遗憾,上一篇文章,评论没有超过 50 ,所以本篇博客,送 2 份~

只要评论数过 50
随机抽取一名幸运读者
奖励 39.9 元爬虫 100 例专栏 1 折购买券一份,只需 3.99 元

今天是持续写作的第 164 / 200 天。可以点赞 、评论、收藏啦 。

文章来源于网络 ,如有侵权请联系站长QQ61910465删除
本文版权归去快排wWw.seogUrublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系qq❉61910465