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

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);

猜你喜欢

转载自www.cnblogs.com/mxh-java/p/10747808.html