'''
爬取页面图片
'''
import re
import requests
basepath = "D:/PyDownload/"
if __name__ == "__main__":
resp = requests.get("http://www.163.com")
html = resp.text
pattern = re.compile("<img.* src=\"(https?://.*?)\".*>")
reslist = pattern.findall(html)
for i in range(len(reslist)):
imgUrl = reslist[i]
print(imgUrl)
try:
resp = requests.get(imgUrl)
bytes = resp.content
filename = basepath + str(i) + ".jpg"
file = open(filename, "xb")
file.write(bytes)
file.close()
print(i, "下载完成!")
except Exception as e:
print(e)
pass
print("main over")