Use the simplest statements to achieve the most practical features
Learn python@ small face Dragon
Work for three months, finally tidied up his lazy strength, shake off a lot of reasons, once again share their learning experience, open a special topic, and everyone learn python, acquisition The simplest command, realize the most practical functions, I beginners, if there is a fallacy to look more correct.
Introduction to the development environment: Ubuntu 11.04, Sublime2, Python2.7
Libraries involved: OS, urllib
Import OS
os.listdir ('./') #显示 './' files and folders under current path
os.chdir ('/home ') #切换路径至home目录
os.getcwd () # Get current path
Of course, there are a lot of OS libraries, and see Python Docs the above are a few of the APIs you might need
Import urlib
ufile = Urllib.urlopen (URL) # Get a network Connection object
info = ufile.info () # URL Content-related meta-info
if Info.gettype () = = ' text/html ': #获取网站相应类型内容
urlhandler= urllib. Urlopener () #创建一个urlopen实例
urlhandler.retrieve (url,filename); #获取url并保存为filename返回
Goal: Find a cartoon website to go to the next comic book
Realize:
1 First, find a comic site, to determine the link to find pictures, it is not easy, now many do not write the image address in the code. Through Google pictures, or was I found one: http://www.acmoe.com/comicdata/2943/%e6%9c%aa%e5%a9%9a%e5%85%b3%e7%b3%bb[%e9%9d%92%e5%87%ba% E4%ba%8e%e8%93%9d]_vol_1/1.jpg
In other words, the first volume will be down all the time.
2 There is to consider the 404 error, the number of a volume is limited, more than will be reported 404, so to add error handling
3 on the code:
Import sys
import OS
import urllib
def downloadcatoon (url,dir,vol,catoontype= ' jpg '):
Volmax = 200 #最大页数
catoonnum = 1
os.chdir (dir) #移至dir目录下
Os.mkdir (vol.) #创建vol1, 2, ...
Os.chdir (Os.path.join (dir, vol.)) #进入该目录 for
i in range (Volmax):
image = Urllib. Urlopener ()
catoonname = str (catoonnum + i) + '. ' + catoontype
try:
image.retrieve (url + catoonname, Catoo Nname)
pass
except IOError: #当遇到错误.
print ' job ' break
def main ():
args = sys.argv[1:]
downloadcatoon (args[0], args[1], ARGS[2])
if __name__ = = ' __main__ ':
Main ()
4 test
Python downloadcartoon.py http://www.acmoe.com/comicdata/2943/%e6%9c%aa%e5%a9%9a%e5%85%b3%e7%b3%bb[%e9%9d%92%e5% 87%ba%e4%ba%8e%e8%93%9d]_vol_1/./vol1
Okay, well, take care of it, go to sleep, have time to try downloading online novels, and create benefits for the otaku.