Skip to content

二、爬取图片、音频和文档

1、爬取图片

python
import time
import requests
import random
from fake_useragent import UserAgent

url='https://img1.baidu.com/it/u=2233719353,1660282400&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=720'

headers={
    'User-Agent':UserAgent().random
}

response=requests.get(url=url,headers=headers)

with open(f"./爬取资源/图片/{int(time.time()*1000)}.png","wb") as fs:
    fs.write(response.content)

2、爬取音频

python
url = 'https://m10.music.126.net/20240126110618/5b8a01f231c99763c0ef176be7694171/yyaac/obj/wonDkMOGw6XDiTHCmMOi/14052039430/3821/4f58/6c99/6af13d65f095a7e51bcebddf9cc5a9d8.m4a'

headers = {
    'User-Agent': UserAgent().random
}

response = requests.get(url=url, headers=headers)

with open(f"./爬取资源/音乐/{int(time.time() * 1000)}.mp3", "wb") as fs:
    fs.write(response.content)

3、爬取视频

python
url='https://720930798.qnqcdn.net:22443/qn-32Hd6SlLHKCFWIBAkVCUkI1EnGmQUMT4.vodkgeyttp8.vod.126.net/cloudmusic/e0c3/core/c425/a0ebb346a2820d30b614068c0faba244.mp4?wsSecret=46e3a8484302e1f16acde9c45b43fbcc&wsTime=1706236278'

headers={
    'User-Agent':UserAgent().random
}

response=requests.get(url="https://720930798.qnqcdn.net:22443/qn-32Hd6SlLHKCFWIBAkVCUkI1EnGmQUMT4.vodkgeyttp8.vod.126.net/cloudmusic/e0c3/core/c425/a0ebb346a2820d30b614068c0faba244.mp4?wsSecret=46e3a8484302e1f16acde9c45b43fbcc&wsTime=1706236278",headers=headers)

with open(f"./爬取资源/视频/{int(time.time()*1000)}.mp4",'wb') as fs:
    fs.write(response.content)