在爬图片网站的图片,要把"src="后面的链接取出来,但是一直取出来是空的,不知道正则表达式哪里有错!
要爬取的链接:
想要得到的结果:
//img.ivsky.com/img/tupian/li/202107/15/xingkong-005.jpg
目前的正则写法:
ex = '
.*?<img src="(.*?)" alt.*?
'img_src_list = re.findall(ex,page_text,re.S)
完整的代码:
import requests
import re
import os
if __name__ == "__main__":
if not os.path.exists('./otherLibs'):
os.mkdir('./otherLibs')
url = 'https://www.ivsky.com/tupian/ziranfengguang/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
}
page_text = requests.get(url=url,headers=headers).text
ex = '<div class="il_img">.*?<img src="(.*?)" alt.*?</div>'
img_src_list = re.findall(ex,page_text,re.S)
for src in img_src_list:
src = 'https:'+src
iamge_data = requests.get(url=url,headers=headers).content
img_name = src.split('/')[-1]
img_Path= './otherLibs'+img_name
with open(img_Path,'wb') as fp:
fp.write(iamge_data)
print(img_name,'下载成功')