python urllib
urllib.urlretrieve(url[,filename[,reporthook[,data]]]):
urlretrieve方法直接将远程数据下载到本地
filebane指定了保存文件的路径 如果没指定会生成一个临时的文件去保存
reporthook是一个函数 当链接上服务器以及相应的数据块传输完毕的时候会出发该函数
实现一个小蜘蛛:
import urllib
import re
def getHtml(url):
page=urllib.urlopen(url)
html=page.read()
page.close()
return html
def getImg(html):
reg='src="(.*?.jpg)" width'
imgre =re.compile(reg)
imglist-imgre.findall(html)
i=0
for imgurl in imglist:
urllib.urlretrieve(imgurl,"/root/img/%d.jpg"%(i,))
i+=1
ht=getHtml(r"http://tieba.baidu.com/p/16992709597?fr=ala0&alath=2&pstbala=1")
print "##################"
print getImg(ht)
import chardet
chardet.detect(page) 得到的是一个字典 detect()检测所给字符的编码(可信度,编码)

浙公网安备 33010602011771号