zoukankan      html  css  js  c++  java
  • python接口自动化测试-requests下载图片

    如:

    import requestsdef down_image():
        '''
        demo:下载图片
        '''
        headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}
        url = 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2160522519,3825740687&fm=26&gp=0.jpg'
        response = requests.get(url, headers=headers, stream=True)
        print(response.status_code)
        with open('demo.jpg','wb') as fd:
            for chunk in response.iter_content(128):
                fd.write(chunk)
    def down_image_improved():
        '''
        demo:下载图片
        '''
        #伪造headers信息
        headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}
        #限定url
        url = 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2160522519,3825740687&fm=26&gp=0.jpg'
        response = requests.get(url, headers=headers, stream=True)
        from contextlib import closing
        print(response.status_code)
        #打开传输流后最后会关上
        with closing(requests.get(url, headers=headers, stream=True)) as response:
            #打开文件
            with open('demo.jpg', 'wb') as fd:
                #每128写入一次
                for chunk in response.iter_content(128):
                    fd.write(chunk)
    down_image_improved()

  • 相关阅读:
    蓝桥杯--算法训练 区间k大数查询
    vijos1782:借教室
    vijos1779国王游戏
    C++大数模板
    HDU1042(N!:设4为基数)
    HDU1026(延时迷宫:BFS+优先队列)
    POJ3984(迷宫问题)
    HDU3018:Ant Trip(欧拉回路)
    HDU5438:Ponds(拓扑排序)
    2008北航:字符串匹配
  • 原文地址:https://www.cnblogs.com/peiya/p/12542946.html
Copyright © 2011-2022 走看看