爬取图片
源码
import requests
import re
def html_code(url):
res = requests.get(url=url)
html = res.content.decode()
return html
def img_path_list(html):
return re.findall(r"style/\w*\.jpg",html)
def img_request(img_path_list):
img_list = []
for img_path in img_path_list:
img_list.append(requests.get(url+img_path))
return img_list
def img_download(img,i):
img_save_path = f"./image/{i}.jpg"
print(img_save_path )
with open(img_save_path,"wb") as f:
f.write(img.content)
url = "http://10.9.47.154/python-spider/"
html=html_code(url)
img_path_list=img_path_list(html)
imgs = img_request(img_path_list)
i=0
for img in imgs:
img_download(img,i)
i+=1
效果