Python爬虫入门教程 8-100 蜂鸟网图片爬取之三

简介: 1. 蜂鸟网图片-啰嗦两句前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用aiohttp 希望你喜欢爬取页面https://tu.fengniao.com/15/ 本篇教程还是基于学习的目的,为啥选择蜂鸟,没办法,我瞎选的。

1. 蜂鸟网图片-啰嗦两句

前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用aiohttp 希望你喜欢
爬取页面https://tu.fengniao.com/15/ 本篇教程还是基于学习的目的,为啥选择蜂鸟,没办法,我瞎选的。

image

一顿熟悉的操作之后,我找到了下面的链接
https://tu.fengniao.com/ajax/ajaxTuPicList.php?page=2&tagsId=15&action=getPicLists

这个链接返回的是JSON格式的数据

  1. page =2页码,那么从1开始进行循环就好了
  2. tags=15 标签名称,15是儿童,13是美女,6391是私房照,只能帮助你到这了,毕竟我这是专业博客 ヾ(◍°∇°◍)ノ゙
  3. action=getPicLists接口地址,不变的地方

2. 蜂鸟网图片-数据有了,开爬吧

import aiohttp
import asyncio

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
           "X-Requested-With": "XMLHttpRequest",
           "Accept": "*/*"}

async def get_source(url):
    print("正在操作:{}".format(url))
    conn = aiohttp.TCPConnector(verify_ssl=False)  # 防止ssl报错,其中一种写法
    async with aiohttp.ClientSession(connector=conn) as session:  # 创建session
        async with session.get(url, headers=headers, timeout=10) as response:  # 获得网络请求
            if response.status == 200:  # 判断返回的请求码
                source = await response.text()  # 使用await关键字获取返回结果
                print(source)
            else:
                print("网页访问失败")


if __name__=="__main__":
        url_format = "https://tu.fengniao.com/ajax/ajaxTuPicList.php?page={}&tagsId=15&action=getPicLists"
        full_urllist= [url_format.format(i) for i in range(1,21)]
        event_loop = asyncio.get_event_loop()   #创建事件循环
        tasks = [get_source(url) for url in full_urllist]
        results = event_loop.run_until_complete(asyncio.wait(tasks))   #等待任务结束

image

上述代码在执行过程中发现,顺发了20个请求,这样子很容易就被人家判定为爬虫,可能会被封IP或者账号,我们需要对并发量进行一下控制。
使Semaphore控制同时的并发量

import aiohttp
import asyncio
# 代码在上面
sema = asyncio.Semaphore(3)
async def get_source(url):
    # 代码在上面
    #######################
# 为避免爬虫一次性请求次数太多,控制一下
async def x_get_source(url):
    with(await sema):
        await get_source(url)

if __name__=="__main__":
        url_format = "https://tu.fengniao.com/ajax/ajaxTuPicList.php?page={}&tagsId=15&action=getPicLists"
        full_urllist= [url_format.format(i) for i in range(1,21)]
        event_loop = asyncio.get_event_loop()   #创建事件循环
        tasks = [x_get_source(url) for url in full_urllist]
        results = event_loop.run_until_complete(asyncio.wait(tasks))   #等待任务结束

走一波代码,出现下面的结果,就可以啦!
image

在补充上图片下载的代码

import aiohttp
import asyncio

import json

# 代码去上面找
async def get_source(url):
    print("正在操作:{}".format(url))
    conn = aiohttp.TCPConnector(verify_ssl=False)  # 防止ssl报错,其中一种写法
    async with aiohttp.ClientSession(connector=conn) as session:  # 创建session
        async with session.get(url, headers=headers, timeout=10) as response:  # 获得网络请求
            if response.status == 200:  # 判断返回的请求码
                source = await response.text()  # 使用await关键字获取返回结果
                ############################################################
                data = json.loads(source)
                photos = data["photos"]["photo"]
                for p in photos:
                    img = p["src"].split('?')[0]
                    try:
                        async with session.get(img, headers=headers) as img_res:
                            imgcode = await img_res.read()
                            with open("photos/{}".format(img.split('/')[-1]), 'wb') as f:
                                f.write(imgcode)
                                f.close()
                    except Exception as e:
                        print(e)
                ############################################################
            else:
                print("网页访问失败")


# 为避免爬虫一次性请求次数太多,控制一下
async def x_get_source(url):
    with(await sema):
        await get_source(url)


if __name__=="__main__":
        #### 代码去上面找

图片下载成功,一个小爬虫,我们又写完了,美滋滋

微信搜索htmlhttp 发现不一样的惊喜~

9150e4e5ly1fw2rlx3wshg20dc0dcmyw.gif

相关文章
|
7天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
11天前
|
数据采集 Python
【python】爬虫-西安医学院-校长信箱
本文以西安医学院-校长信箱为基础来展示爬虫案例。来介绍python爬虫。
【python】爬虫-西安医学院-校长信箱
|
17天前
|
数据采集 安全 Python
python并发编程:Python实现生产者消费者爬虫
python并发编程:Python实现生产者消费者爬虫
24 0
python并发编程:Python实现生产者消费者爬虫
|
29天前
|
JSON C语言 C++
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
60 1
|
1天前
|
机器学习/深度学习 算法 自动驾驶
opencv python 图片叠加
【4月更文挑战第17天】
|
12天前
|
数据采集 存储 前端开发
Python爬虫如何快速入门
写了几篇网络爬虫的博文后,有网友留言问Python爬虫如何入门?今天就来了解一下什么是爬虫,如何快速的上手Python爬虫。
17 0
|
25天前
|
数据采集 存储 Web App开发
一键实现数据采集和存储:Python爬虫、Pandas和Excel的应用技巧
一键实现数据采集和存储:Python爬虫、Pandas和Excel的应用技巧
|
27天前
|
数据采集 前端开发 JavaScript
Python爬虫零基础到爬啥都行
Python爬虫项目实战全程实录,你想要什么数据能随意的爬,不管抓多少数据几分钟就能爬到你的硬盘,需要会基本的前端技术(HTML、CSS、JAVASCRIPT)和LINUX、MYSQL、REDIS基础。
20 1
Python爬虫零基础到爬啥都行
|
29天前
|
算法 程序员 C++
【Python 基础教程 运算符06】Python3运算符超详细解析:全面入门教程,初学者必读
【Python 基础教程 运算符06】Python3运算符超详细解析:全面入门教程,初学者必读
90 2
|
29天前
|
算法 程序员 C++
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
93 1