堆糖网热门图片下载[通俗易懂](数据结构中堆与内存堆区的区别)
生活随笔
收集整理的這篇文章主要介紹了
堆糖网热门图片下载[通俗易懂](数据结构中堆与内存堆区的区别)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
大家好,又見面了,我是你們的朋友風君子。
下載目標是堆糖網熱門圖片,打開網頁并下拉發現圖片是通過ajax加載的,按F12打開開發者工具選擇nerwork并篩選xhr,繼續下拉網頁找到ajax請求的api,如下圖所示
然后就可以構造請求獲取包含圖片url的json數據,對于網絡請求等IO密集型任務,開啟進程池可以提高下載速度
代碼如下:
import requests from requests import exceptions import re from multiprocessing import Pool import os def get_pic_info(): url = 'https://www.duitang.com/napi/index/hot/?' for i in range(1000): params = { 'include_fields': 'top_comments,is_root,source_link,item,buyable,root_id,status,like_count,sender,album', 'limit': '24', 'start': 24 * i, } response = requests.get(url, params=params) json_data = response.json() pic_list = json_data['data']['object_list'] for pic_ in pic_list: image = {} pic_info = pic_['album'] pic_url = pic_info['covers'][0] image['pic_name'] = re.sub(r'[\\/:*?"<>|\r\n。,.? ]+', '', pic_info['name']) + '.' + pic_url.split('.')[-1] image['pic_url'] = pic_url yield image def download_pic(image): if not os.path.exists(f'./img/{image["pic_name"]}'): try: resp = requests.get(image['pic_url']) if resp.status_code == 200: with open(f'./img/{image["pic_name"]}', 'wb') as f: f.write(resp.content) except exceptions: return None else: print(image['pic_name'] + ' has already downloaded') if __name__ == '__main__': if not os.path.exists('./img'): os.mkdir('./img') pool = Pool() pool.map(download_pic, get_pic_info()) pool.close() pool.join()
總結
以上是生活随笔為你收集整理的堆糖网热门图片下载[通俗易懂](数据结构中堆与内存堆区的区别)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: open ai gpt_您实际上想尝试的
- 下一篇: python 线性回归_Python中的