python 爬虫 下载百度美女图片

因为要从网上下载很多图片,一张一张的复制下载速度很慢。

  • 爬虫实现方式
  1. 查找到访问图片的链接URI
  2. 访问URI获取到图片的链接
  3. 访问图片的链接,并保存图片到本地

废话不多说 上代码

import requests
import json

def get_image_url():
    url = 'https://image.baidu.com/search/index?tn=resulttagjson&logid=10358070151245603719&ie=utf-8&fr=&word=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&ipn=r&fm=index&pos=history&queryWord=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&cl=2&lm=-1&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=1&latest=&copyright=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=true&pn=0&rn=30&gsm=5a&1713426180895='
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
    }
    response = requests.get(url, headers=headers)
    download_image = dict()
    if response.status_code == 200:
        data = response.text
        json_data = json.loads(data)['data']
        for image in json_data:
            if image:
                download_image[image['fromPageTitle']] = image['thumbURL']

        return download_image


def download(image,title):
    response = requests.get(image)
    with open('image/' + title + '.jpg', mode="bw") as f:
        f.write(response.content)

if __name__ == '__main__':
    download_image = get_image_url()
    for title, image in download_image.items():
        title = title.replace('"','').replace('|','').replace('/','')
        download(image, title)
  • 找到下载图片的文件夹,哈哈 美女图片可以独自欣赏啦
posted @   低头不见抬头见  阅读(69)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示