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