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