Find some beautiful pictures when I browse Baidu Post bar,i want to download them. But it's troublsome to do, by Hand,so I write a python.
#Coding=utf-8Importurllib.requestImportRedefgethtml (URL): page=urllib.request.urlopen (URL) HTML=Page.read ()returnHTMLdefgetimg (HTML): Reg=r'src= "(http://imgsrc\.baidu\.com.+?\.jpg)"'Imgre=Re.compile (reg) Imglist=Re.findall (imgre,html) x=0 forImgurlinchImglist:urllib.request.urlretrieve (Imgurl,'bizhi\%s.jpg'%x) x=x+1Print("Download OK") HTML=gethtml ("http://tieba.baidu.com/p/3868899385") HTML=html.decode ('Utf-8') getimg (HTML)
Code is very easy,there is some function:
Urlib.request.urlopen (URL, data=none, [Timeout,]*, Cafile=none, Capath=none, Cadefault=false, Context=none)
For HTTP or HTTPS URLs, 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 the pattern in string, as a list of strings
Python---download Baidu Post Bar picture