import re import urllib.request # 爬取网页 def getHtml(url): page=urllib.request.urlopen(url) html=page.read() return html # 获取图片地址 def getImg(html): rule=r‘src="(.*?\.jpg)" width‘ rule_compile=re.compile(rule) Img_list=re.findall(rule_compile,html.decode(‘GBK‘)) return Img_list url="http://desk.zol.com.cn/bizhi/7005_87014_2.html" html=getHtml(url) # print(getImg(html)) # 下载图片到当前目录 x=0 for i in getImg(html): urllib.request.urlretrieve(i,‘%s爬虫.jpg‘ % x) x+=1
时间: 2024-12-17 22:30:52