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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM