python下载页面文件

#!/usr/bin/python3
# *-* coding:utf-8 *-*
import asyncio

import requests


async def download(url):
    """"""
    print('%s下载中' % url)
    await asyncio.sleep(1)  # 休眠
    resp = requests.get(url)
    return resp.content, resp.status_code
    pass


@asyncio.coroutine
def write_file(filename, content):
    """"""
    with open(filename, 'wb') as f:
        f.write(content)
    pass


@asyncio.coroutine
def save(url, filename):
    """"""
    content, code = yield from download(url)
    print(url, code)
    yield from write_file(filename, content)
    print(url, filename, '保存成功')


if __name__ == "__main__":
    # 获取事件循环器对象
    url = "http://www.hbt7.com/hbinfo/plugins/font-awesome-4.1.0/css/font-awesome.min.css"
    # f_name = "images/"+url.split('images/')[1]
    # f_name = "js/"+url.split('js/')[1]
    f_name = "plugins/"+url.split('plugins/')[1]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait([
        # save('https://www.baidu.com', 'baidu.html'),
        # save('https://jd.com', 'jd.html'),
        # save('https://mail.qq.com', 'qq.html'),

        save(url, f_name),
    ]))
posted @ 2022-06-17 10:13  ALin_Da  阅读(138)  评论(0编辑  收藏  举报