PythonDemo1--爬网页图片
2017-02-09 本文已影响53人
DDY
利用Python爬网页上图片
#!/usr/bin/python
#coding=utf-8
import urllib
import re
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?\jpeg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre, html)
x = 0
'''
for 循环遍历list列表,利用urlretrieve()方法从远程直接下载到本地
'''
for imgurl in imglist:
urllib.urlretrieve(imgurl,'%s.jpeg' %x)
x+=1
html = getHtml("http://mt.sohu.com/20170120/n479174254.shtml")
print getImg(html)
img.png