+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-09(1)

谁知盘中餐,粒粒皆辛苦

发布于2020-09-14 22:53   下载积分(1)   下载次数(0)

下载

0

1

2

3

4

某度的
import requests
import re
import os
import time

def download_pic(html):
    ''' 获取图片下载链接 '''
    # 设置正则表达式,查找图片链接
    pat1 = r'"objURL":"(.*?)",'
    pic_url = re.findall(pat1, html)
    # 创建保存路径文件夹
    path = 'path'
    if not os.path.exists(path):
        os.makedirs(path)
    # 下载图片
    for url in pic_url:
        r = requests.get(url)
        name = url[-10:]
        filename = path + '/' + name
        print("正在下载图片:%s" % name)
        try:
            with open(filename, 'wb') as f:
                f.write(r.content)
                time.sleep(1)
        except Exception as e:
            print("图片下载出错!", e)

def get_html(url):
    # 设置代理头
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Upgrade-Insecure-Requests': '1'
    }
    html = requests.get(url, headers=headers).text
    return html

def main():
    word = input("请输入你需要的名称:")
    # 构造URL
    for i in range(1, 10):
        url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn=' + str(i * 20) + '&ct=201326592&v=flip'
        html = get_html(url)
        download_pic(html)

if __name__ == '__main__':
    main()

0

1

2

3

4

5

6

7

8



所属网站分类: 资源下载 > python项目实战

作者:猛兽侠qq1046479518

链接: https://www.pythonheidong.com/blog/article/522117/d3f59067f53360bf1ff2/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)