python爬虫简单爬取网页图片模板
import requests
import re
def getHTMLTxet(url):
try:
r=requests.get(url)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
print("")
'''
此处的r'<img src=".?">'为爬取网页源码中以<img src="开头以"结尾
例如:<img src="xxx.jpg"
'''
def proper(list,html):
ls=re.findall(r'<img src=".*?"',html) #此处r表达式需要修改
for i in range(len(ls)):
list.append(ls[i].split('"')[1])
def main():
url='此处为要爬取网页图片的网址' #此处爬取网页网址需要填进去
text=getHTMLTxet(url)
jpglist=[]
proper(jpglist,text)
x=0
for i in jpglist:
x+=1
r=requests.get(i)
with open('照片3/'+str(x)+'.jpg','wb') as f:
f.write(r.content)
f.close()
print('{}保存成功'.format(x))
main()