Wallhaven|为什么都夸它是世界上最惊艳的壁纸网站?也许你之前还在通过百度搜索壁纸,或者是使用国内的一些图片资源站,相信你也曾被那些复杂的积分制度或者收费方式折磨不堪,但有了 Wallhaven 之后,你所要做的就是把这个网站收藏好就行了。这次,我们将尝试通过简单的代码保存全站的图片~~~(介于没有WiFi,只做部分下载)


import time
from pyquery import PyQuery
import os
import asyncio
import aiohttp
import warnings
async def url_pages(page):
async with semaphore:
_url = 'https://wallhaven.cc/toplist?page={}'.format(page)
async with session.get(_url) as response:
result = await response.text()
status = response.status
while status == 429:
await asyncio.sleep(2)
async with session.get(_url) as resp:
result = await resp.text()
status = resp.status
for _item in PyQuery(result)('.thumb-listing-page li').items():
async with lock:
img_url.append(_item('.preview').attr('href'))
async def get_img_link(_url):
async with session.get(_url) as response:
html = await response.text()
status = response.status
while status == 429:
await asyncio.sleep(2)
async with session.get(_url) as resp:
html = await resp.text()
status = resp.status
_url = PyQuery(html)('#wallpaper').attr('src')
img_name = os.path.basename(_url)
return _url, img_name
async def img_save():
async with semaphore:
flag = 3
while flag:
if not len(img_url):
await asyncio.sleep(5)
flag = flag - 1
continue
_url = img_url.pop(0)
url, name = await get_img_link(_url)
global number
number = number + 1
print("下载第{}张 --> {}".format(number, name))
if os.path.exists(name):
continue
async with session.get(url) as res:
with open(name, 'wb') as f:
f.write(await res.read())
async def scrape_main():
global session
session = aiohttp.ClientSession()
scrape_index_tasks = [asyncio.ensure_future(img_save()) for i in range(20)]
scrape_index_tasks.extend([asyncio.ensure_future(url_pages(page)) for page in range(1, pages+1)])
await asyncio.wait(scrape_index_tasks)
await session.close()
print("任务爬取结束O(∩_∩)O~ 共爬取{}张图片".format(number))
if __name__ == '__main__':
warnings.filterwarnings("ignore", category=DeprecationWarning)
semaphore = asyncio.Semaphore(30)
start_time = time.time()
save_path = os.getcwd() + "\wallpaper"
if not os.path.exists(save_path):
os.mkdir(save_path)
os.chdir(save_path)
number = 0
img_url = []
session = None
lock = asyncio.Lock()
pages = 3
asyncio.get_event_loop().run_until_complete(scrape_main())
print(f"累计耗时{time.time()-start_time:.2f}s")
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步