使用beautifulsoup + request 爬取div背景图片,因为是图片是在style的background-image,这里需要去截取图片地址
文件保存在imgs文件夹里,代码里没写创建文件夹,需要先手动建一个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
from bs4 import BeautifulSoup import requests
r = requests.get('http://cn.60wdb.com/items/c/2') soup = BeautifulSoup(r.text)
for k in soup.find_all('div',class_="icon"): value = k.attrs['style'] img_url = value[22:value.rfind(')', 1)] img_name = img_url[57:len(img_url)] img_content = requests.get(img_url) if img_content.status_code == 200: img = open('imgs/'+img_name, 'wb') img.write(img_content.content) img.close()
|