python3爬取网页图片路径并写入文件

时间:2021-06-12 14:42:13
import re
import urllib.request # 获取网页文件
def getHtml(url):
response = urllib.request.urlopen('https://www.zhipin.com/?ka=header-home');
return response.read(); # 写入数据到文件
def writeFile(fileName,data):
# 打开文件方式为'a'可不覆盖原有数据
htmlFile = open(fileName, 'a');
htmlFile.write(data);
htmlFile.close(); # 截取后缀为.jpg的图片
def getImgSrc(fileName):
# decode()将string转为byte
imgUrl = re.findall(r'https:.+\.jpg',fileName.decode('utf-8'));
return imgUrl; html = getHtml('https://www.imooc.com/');
print(html); imgUrl = getImgSrc(html);
for i in imgUrl:
print(i);
writeFile('imgUrl.txt', i);