import urllib
from urllib import request
import re
import chardet
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext' #???
imgre = re.compile(reg)
## encode_type = chardet.detect(html) #获得字符串的编码格式
## html = html.decode(encode_type['encoding']) #以encode_type格式对html进行解码
encode_type = chardet.detect(html)['encoding']
html = html.decode(encode_type)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'D:\\abcd\\'+'%s.jpg' % x)
##自定义下载的图片的存贮路径D:abcd文件夹
x+=1
return imglist
html = getHtml("http://tieba.baidu.com/p/2460150866")
from urllib import request
import re
import chardet
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'src="(.+?\.jpg)" pic_ext' #???
imgre = re.compile(reg)
## encode_type = chardet.detect(html) #获得字符串的编码格式
## html = html.decode(encode_type['encoding']) #以encode_type格式对html进行解码
encode_type = chardet.detect(html)['encoding']
html = html.decode(encode_type)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'D:\\abcd\\'+'%s.jpg' % x)
##自定义下载的图片的存贮路径D:abcd文件夹
x+=1
return imglist
html = getHtml("http://tieba.baidu.com/p/2460150866")
print (getImg(html))