代码
import urllib.request
import requests
from lxml import etree
url = "https://699pic.com/tupian/fengjing.html"
def schedule(blocknum,blocksize,totalsize):
per = 100*blocknum*blocksize/totalsize
if per>100:
per=100
print('下载进度:%d'%(per))
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36'}
response = requests.get(url,headers=headers)
if response.status_code==200:
data = response.text
with open('.\page\img.html','w',encoding='utf-8') as w:
w.write(data)
html = etree.HTML(data)
img_url = html.xpath('//img/@src')
if len(img_url)>0:
for index,img in enumerate(img_url):
urllib.request.urlretrieve('http:'+img,'./images/img'+str(index)+'.jpg',schedule)
print('--------->下载第',index,'张图片完毕')
爬取效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/95e9cecc412b46155d9dbeceb3dc80cf.png)
爬取结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9b3eaca2b7441fe36985bee8b2f1bf5d.png)