fresh_python 发表于 2020-5-20 19:46:47

爬取52张520图片

本帖最后由 fresh_python 于 2020-5-20 19:48 编辑

今天5月20号,献上爬取52张520图片的代码。(图片来源于百度图片)如有不足欢迎大家指正import json
import requests
import os
path = os.getcwd()
photo_path = path+os.sep+'520图片'
try:
    os.mkdir(photo_path)
except:
    pass
def save(url,each):#保存图片
    try:
      response = requests.get(url)
      filename = photo_path+os.sep+str(each)+'.jpg'
      print('正在下载图片%d'%(each+1))
      
      with open(filename,'wb') as f:
            f.write(response.content)
    except:
      print('下载失败')
def main():
   
    url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=0%2C0&fp=detail&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=0&lpn=0&st=undefined&word=520&z=0&ic=undefined&hd=undefined&latest=undefined&copyright=undefined&s=undefined&se=&tab=0&width=undefined&height=undefined&face=undefined&istype=0&qc=&nc=&fr=&simics=&srctype=&bdtype=0&rpstart=0&rpnum=0&cs=2865499277%2C3199044538&catename=&force=undefined&cardserver=&tabname=&pn=0&rn=60&gsm=1f&1589961222385='

    response = requests.get(url)
    data = json.loads(response.text)

    for each in range(52):
      url1 = str(data['data']['hoverURL'])
      save(url1,each)
if __name__=='__main__':
    main()
页: [1]
查看完整版本: 爬取52张520图片