python 爬蟲 下載百度美女圖片

低头不见抬头见發表於2024-04-18

因為要從網上下載很多圖片,一張一張的複製下載速度很慢。

  • 爬蟲實現方式
  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)
  • 找到下載圖片的資料夾,哈哈 美女圖片可以獨自欣賞啦

相關文章