1. 程式人生 > 實用技巧 >python爬取堆糖網每日精選圖片

python爬取堆糖網每日精選圖片

前言

本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯絡我們以作處理。

基本環境配置

  • python 3.6
  • pycharm
  • requests

pip install requests

實現程式碼

import requests
url = 'https://www.duitang.com/napi/vienna/feed/list/by_common/?start=0&limit=18'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
' } response = requests.get(url=url, headers=headers) html_data = response.json() dit = html_data['data']['object_list'] for i in dit: img_data = i['atlas']['blogs'] for j in img_data: img_url = j['photo']['path'] name = img_url.split('_')[-1] img_url_response = requests.get(url=img_url, headers=headers) with open(
'G:\\python\\demo\\案例\\堆糖網\\圖片\\' + name, mode='wb') as f: f.write(img_url_response.content) print(img_url)

實現效果