Find some beautiful pictures when i browse Baidu Post Bar,I want to download them. But it is troublsome to do that by hand,so i write a python.
#coding=utf-8 import urllib.request import re def getHtml(url): page=urllib.request.urlopen(url) html=page.read() return html def getImg(html): reg=r‘src="(http://imgsrc\.baidu\.com.+?\.jpg)"‘ imgre=re.compile(reg) imglist=re.findall(imgre,html) x=0 for imgurl in imglist: urllib.request.urlretrieve(imgurl,‘bizhi\%s.jpg‘ % x) x=x+1 print ("download ok") html=getHtml("http://tieba.baidu.com/p/3868899385") html=html.decode(‘utf-8‘) getImg(html)
Code is very easy,there are some function:
Urlib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
For http or https url ,this function return a http.client.HTTPResponse object slightly modified.
urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
Copy a network object denoted by url to locail file
re.compile(pattern,flags0)
Compile a regular expression pattern into a regular express object
re.findall(pattern,string,flags=0)
Return all non-overlapping matches of pattern in string ,as a list of strings
时间: 2024-10-14 10:55:54