2007年07月15日 星期日 04:41
ËäÈ»°³ÖªµÀдµÃºÜÀã¬ÔÙ£¬°³´Óûд¹ýʲôPY´úÂ룬ÿһ´Îд¶¼²»»á³¬¹ý30ÐеĴúÂëÓ¦ÓᣠÌû³öÀ´Èôó¼ÒÅúÆÀ¡£ÕâÊǰ³ÔÚ¿´¡¶Dive in Python¡·Ê±²Î¿¼Ð´µÄ¡£ÐÂÊÖ³õѧ£¬Ï£ÍûµÃµ½´ó¼Ò¶à¶àÖ¸µã£¬ÃâµÃ°³×ßÈëħµÀ¡£:) # coding: utf-8 #!/usr/bin/env python import urllib2, httplib # import time from openanything import SmartRedirectHandler class DjangoDocumentDownload: target_dir = 'e:\\project\\python\\django\\docment\\files\\' httplib.HTTPConnection.debuglevel = 1 def document_download(self): urllist = ['overview', 'install', 'tutorial01', 'tutorial02', 'tutorial03', 'tutorial04', 'faq', 'documentation', 'model-api', 'db-api', 'transactions', 'templates', 'templates_python', 'newforms', 'forms', 'sessions', 'cache', 'i18n', 'middleware', 'settings', 'url_dispatch', 'request_response', 'generic_views', 'authentication', 'add_ons', 'databrowse', 'syndication_feeds', 'flatpages', 'redirects', 'sites', 'sitemaps', 'webdesign', 'modpython', 'fastcgi', 'apache_auth', 'static_files', 'email', 'legacy_databases', 'outputting_pdf', 'outputting_csv' ] print "\nHTML request start:\n" for i in urllist: url = 'http://www.djangoproject.com/documentation/'+ i + '/' request = urllib2.Request(url) request.add_header( 'User-Agent', 'openanthing/1.0 + Ken.Django.player在China' ) opener = urllib2.build_opener(SmartRedirectHandler) try: htmldata = opener.open(request).read() except: print "sorry....'%s' load faild!\n\n" % i break local_url = self.target_dir + i + '.html' try: f = open(local_url, 'w') f.write(htmldata.replace('http://media.djangoproject.com/', '')) except: print "sorry! create file faild! please try again!" break finally: f.close() htmldata = '' print "created file: '.../%s.html'" % i # time.sleep(1) def css_download(self): css_urls = ['base.css', 'print.css'] print "\nCSS request start:\n" for i in css_urls : request = urllib2.Request('http://media.djangoproject.com/css/'+i) request.add_header( 'User-Agent', 'openanthing/1.0 + Ken.Django.player在China' ) opener = urllib2.build_opener(SmartRedirectHandler) try: cssdata = opener.open(request).read() except: print i, " request faild." break f = open(self.target_dir +'css\\'+ i, 'w') try: f.write(cssdata) except: print i, " write faild!" break finally: f.close() print i, " create cuccess!" if __name__ == '__main__': django_go = DjangoDocumentDownload() django_go.document_download() django_go.css_download() # --------------------------------------- # coding: utf-8 #!/usr/bin/env python import urllib2 class SmartRedirectHandler(urllib2.HTTPRedirectHandler): def http_error_301(self, req, fp, code, msg, headers): result = urllib2.HTTPRedirectHandler.ttp_error_301(self, req, fp, code, msg, headers) result.status = code return result def http_error_302(self, req, fp, code, msg, headers): result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers) result.status = code return result -- ÎÒ×ßµ½Ò»¸öİÉúµÄµØ·½, ¸æËß±ðÈË ÎÒҪȥÁ÷ÀË Å¶£¬ÎÒҪȥÁÆÉË¡¡ Gtalk: iexper(at)gmail.com ÓòÃû¹ýÆÚÁË -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070715/bfa44010/attachment.html
2007年07月15日 星期日 10:31
On 7/15/07, 大郎 <iexper在gmail.com> wrote: > 虽然俺知道写得很烂,再,俺从没写过什么PY代码,每一次写都不会超过30行的代码应用。 > 帖出来让大家批评。这是俺在看《Dive in 嗬嗬嗬,有分享就有进步 > Python》时参考写的。新手初学,希望得到大家多多指点,免得俺走入魔道。:) > > # coding: utf-8 > #!/usr/bin/env python > > import urllib2, httplib > # import time > from openanything import SmartRedirectHandler > > class DjangoDocumentDownload: > > target_dir = > 'e:\\project\\python\\django\\docment\\files\\' > > httplib.HTTPConnection.debuglevel = 1 > > def document_download(self): > urllist = ['overview', 'install', 'tutorial01', 'tutorial02', > 'tutorial03', 'tutorial04', > 'faq', 'documentation', 'model-api', 'db-api', 'transactions', > 'templates', 'templates_python', > 'newforms', 'forms', 'sessions', 'cache', 'i18n', 'middleware', > 'settings', 'url_dispatch', > 'request_response', 'generic_views', 'authentication', > 'add_ons', 'databrowse', 'syndication_feeds', > 'flatpages', 'redirects', 'sites', 'sitemaps', 'webdesign', > 'modpython', 'fastcgi', 'apache_auth', > 'static_files', 'email', 'legacy_databases', 'outputting_pdf', > 'outputting_csv' > ] > > print "\nHTML request start:\n" > > for i in urllist: > url = > 'http://www.djangoproject.com/documentation/'+ i + '/' > request = urllib2.Request(url) > request.add_header( > 'User-Agent', 'openanthing/1.0 + Ken.Django.player在China' > ) > opener = urllib2.build_opener(SmartRedirectHandler) > try: > htmldata = opener.open(request).read() > except: > print "sorry....'%s' load faild!\n\n" % i > break > local_url = self.target_dir + i + '.html' > try: > f = open(local_url, 'w') > f.write(htmldata.replace(' http://media.djangoproject.com/', > '')) > except: > print "sorry! create file faild! please try again!" > break > finally: > f.close() > htmldata = '' > print "created file: '.../%s.html'" % i > # time.sleep(1) > > def css_download(self): > css_urls = ['base.css ', 'print.css'] > > print "\nCSS request start:\n" > > for i in css_urls : > > request = urllib2.Request(' > http://media.djangoproject.com/css/'+ i) > request.add_header( > 'User-Agent', 'openanthing/1.0 + > Ken.Django.player在China' > ) > opener = urllib2.build_opener(SmartRedirectHandler) > try: > cssdata = opener.open(request).read() > except: > print i, " request faild." > break > > f = open(self.target_dir +'css\\'+ i, 'w') > try: > f.write(cssdata) > except: > print i, " write faild!" > break > finally: > f.close() > print i, " create cuccess!" > > > > if __name__ == '__main__': > django_go = DjangoDocumentDownload() > django_go.document_download() > django_go.css_download() > > > # --------------------------------------- > > # coding: utf-8 > #!/usr/bin/env python > > import urllib2 > > class SmartRedirectHandler( urllib2.HTTPRedirectHandler): > def http_error_301(self, req, fp, code, msg, headers): > result = > urllib2.HTTPRedirectHandler.ttp_error_301(self, req, fp, > code, msg, headers) > result.status = code > return result > > def http_error_302(self, req, fp, code, msg, headers): > result = > urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, > code, msg, headers) > result.status = code > return result > > -- > 我走到一个陌生的地方, 告诉别人 我要去流浪 > 哦,我要去疗伤…… > > Gtalk: iexper(at)gmail.com > 域名过期了 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to > python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request在lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > -- '''Time is unimportant, only life important! http://zoomquiet.org blog在http://blog.zoomquiet.org/pyblosxom/ wiki在http://wiki.woodpecker.org.cn/moin/ZoomQuiet scrap在http://floss.zoomquiet.org douban在http://www.douban.com/people/zoomq/ ____________________________________ Pls. use OpenOffice.org to replace M$ Office. http://zh.openoffice.org Pls. use 7-zip to replace WinRAR/WinZip. http://7-zip.org/zh-cn/ You can get the truely Freedom 4 software. '''
Zeuux © 2025
京ICP备05028076号