长明灯打一字,开讲了林志颖,不知火舞游戏
前几天的教程内容量都比较大,今天写一个相对简单的,爬取的还是蜂鸟,依旧采用aiohttp
希望你喜欢
爬取页面https://tu.fengniao.com/15/
本篇教程还是基于学习的目的,为啥选择蜂鸟,没办法,我瞎选的。
一顿熟悉的操作之后,我找到了下面的链接
https://tu.fengniao.com/ajax/ajaxtupiclist.php?page=2&tagsid=15&action=getpiclists
这个链接返回的是json格式的数据
专业博客
ヾ(◍°∇°◍)ノ゙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__": #### 代码去上面找
图片下载成功,一个小爬虫,我们又写完了,美滋滋
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论