python抓取星巴克图片案例
import requests from bs4 import BeautifulSoup r = requests.get('https://www.starbucks.com.cn') soup = BeautifulSoup(r.text,'lxml') imgs = soup.select('.padded-2 > a img[src]') num = 0 for img in imgs: num +=1 # print(img['src']) # 图片链接 image_url = img['src'] headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36' } r = requests.get(image_url, headers=headers) # 下载图片 # 二进制数据需要用r.content 进行提取 # 将图片放在‘demofile’文件夹下,‘demofile’是文件夹的名称,将图片放入该文件夹中,该文件夹与py文件在同一目录下 f = open("demofile/%s.jpg" % num, 'wb') #来个占位符 num会自己加 f.write(r.content) f.close()
也可以使用+号拼接
f = open("demofile/"+str(num)+".jpg", 'wb') # num 转换一下字符串 不然无法拼接