摘要:蜂鸟网图片啰嗦两句前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用希望你喜欢爬取页面本篇教程还是基于学习的目的,为啥选择蜂鸟,没办法,我瞎选的。
1. 蜂鸟网图片-啰嗦两句
前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用aiohttp 希望你喜欢
爬取页面https://tu.fengniao.com/15/ 本篇教程还是基于学习的目的,为啥选择蜂鸟,没办法,我瞎选的。
一顿熟悉的操作之后,我找到了下面的链接
https://tu.fengniao.com/ajax/ajaxTuPicList.php?page=2&tagsId=15&action=getPicLists
这个链接返回的是JSON格式的数据
page =2页码,那么从1开始进行循环就好了
tags=15 标签名称,15是儿童,13是美女,6391是私房照,只能帮助你到这了,毕竟我这是专业博客 ヾ(◍°∇°◍)ノ゙
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)) #等待任务结束
上述代码在执行过程中发现,顺发了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)) #等待任务结束
走一波代码,出现下面的结果,就可以啦!
在补充上图片下载的代码
</>复制代码
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__":
#### 代码去上面找
图片下载成功,一个小爬虫,我们又写完了,美滋滋
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/19545.html
摘要:蜂鸟网图片啰嗦两句前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用希望你喜欢爬取页面本篇教程还是基于学习的目的,为啥选择蜂鸟,没办法,我瞎选的。 1. 蜂鸟网图片-啰嗦两句 前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用aiohttp 希望你喜欢爬取页面https://tu.fengniao.com/15/ 本篇教程还是基于学习的...
摘要:蜂鸟网图片简介今天玩点新鲜的,使用一个新库,利用它提高咱爬虫的爬取速度。上下文不在提示,自行搜索相关资料即可创建一个对象,然后用该对象去打开网页。可以进行多项操作,比如等代码中等待网页数据返回创建线程,方法负责安排执行中的任务。 1. 蜂鸟网图片-简介 今天玩点新鲜的,使用一个新库 aiohttp ,利用它提高咱爬虫的爬取速度。 安装模块常规套路 pip install aiohtt...
摘要:蜂鸟网图片简介今天玩点新鲜的,使用一个新库,利用它提高咱爬虫的爬取速度。上下文不在提示,自行搜索相关资料即可创建一个对象,然后用该对象去打开网页。可以进行多项操作,比如等代码中等待网页数据返回创建线程,方法负责安排执行中的任务。 1. 蜂鸟网图片-简介 今天玩点新鲜的,使用一个新库 aiohttp ,利用它提高咱爬虫的爬取速度。 安装模块常规套路 pip install aiohtt...
阅读 3728·2021-09-22 15:34
阅读 1227·2019-08-29 17:25
阅读 3446·2019-08-29 11:18
阅读 1430·2019-08-26 17:15
阅读 1783·2019-08-23 17:19
阅读 1274·2019-08-23 16:15
阅读 756·2019-08-23 16:02
阅读 1374·2019-08-23 15:19
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要