以知乎上的图片为例https://www.zhihu.com/question/40007169#answer-29511584
找到图片的地址,如
imgsrc="https://pic2.zhimg.com/a45404bcf9cc778c061dd7ca5416b7fd_b.jpg"
写出正则表达式imreg=re.compile('https://pic[0-9]\.zhimg\.com/[a-z 0-9]*_b\.jpg')
找到所有匹配的地址,用urlretrieve函数将图片下载到电脑。
from urllib.request import urlopen
from urllib.request import urlretrieve
import re
def getUrl(url):
html=urlopen(url).read().decode()
imreg=re.compile('https://pic[0-9]\.zhimg\.com/[a-z 0-9]*_b\.jpg')
links=re.findall(imreg,html)
i=0
for link in links:
i+=1
print(link)
path='E:/python/%d.jpg'%i
urlretrieve(link,path)
getUrl('https://www.zhihu.com/question/40007169#answer-29511584')