第13章 Python的HTML应用

本章的知识点:

1、HTML的标签;
2、URL的处理,包含解析、拼合和分解等;
3、URL的编解吗;
4、urllib模块的使用;

内容:

13.1 HTML介绍

13.1.1 HTML的历史

13.1.2 SGML、HTML、XHTML、HTML5的关系

13.1.3 HTML的标签

13.1.4 HTML的框架组件

13.2 URL的处理

13.2.1 统一资源定位符UR

表 13-1 常用的协议
协议名称 协议含义 协议名称 协议含义
 http  超文本传送协议  mailto  电子邮件
 https  使用安全套接层的超文本传送协议  file  本地计算机上的文件
 ftp  文件传输协议  telnet  Telnt协议

 

 

 

 

 

 

13.2.2 URL的解析

表 13-2 urlparse方法返回对象中的属性
属    性 索引值 如果不包含的值 属     性 索引值 如果不包含的值
scheme 0 协议 空字符串 fragment 5 分片部分 空字符串
netloc 1 服务器地址 空字符串 username   用户名 None
path 2 路径 空字符串 password   密码 None
params 3 参数 空字符串 hostname   主机名 None
query 4 查询部分 空字符串 port   端口 None
1 from urllib.parse import urlparse
2 r = urlparse('http://alice:secret@www.hostname.com:80/%7Ealice/python.cgi?query=text#sample')
3 print (r)
4 # 输出:ParseResult(scheme='http', netloc='alice:secret@www.hostname.com:80', path='/%7Ealice/python.cgi', params='', query='query=text', fragment='sample')
View Code
 1 from urllib.parse import urlparse
 2 r = urlparse('http://alice:secret@www.hostname.com:80/%7Ealice/python.cgi?query=text#sample')
 3 print (r)
 4 print (r.scheme)
 5 print (r.netloc)
 6 print (r.path)
 7 print (r.params)
 8 print (r.query)
 9 print (r.fragment)
10 print (r.username)
11 print (r.password)
12 print (r.hostname)
13 print (r.port)
14 print (r.geturl())
15 r2 = urlparse("www.python.org/about","http")
16 print (r2)
17 # 输出:ParseResult(scheme='http', netloc='alice:secret@www.hostname.com:80', path='/%7Ealice/python.cgi', params='', query='query=text', fragment='sample')
18 # http
19 # alice:secret@www.hostname.com:80
20 # /%7Ealice/python.cgi
21 # 
22 # query=text
23 # sample
24 # alice
25 # secret
26 # www.hostname.com
27 # 80
28 # http://alice:secret@www.hostname.com:80/%7Ealice/python.cgi?query=text#sample
29 # ParseResult(scheme='http', netloc='', path='www.python.org/about', params='', query='', fragment='')
View Code

13.2.3 URL的拼合

1 from urllib.parse import urljoin,urlsplit,urlunsplit
2 r = urljoin("http://www.zeroc.com", "ice.html")
3 print (r)
4 r2 = urljoin("","")
5 print (r2)
6 # 输出:http://www.zeroc.com/ice.html
View Code
 1 from urllib.parse import urljoin,urlsplit,urlunsplit
 2 r = urljoin("http://www.zeroc.com", "ftp://www.zeroc.com/ftp")
 3 print (r)
 4 r2 = urljoin("http://www.zeroc.com", "www.zeroc.com/ftp")
 5 print (r2)
 6 r3 = urljoin("http://www.zeroc.com", "http://www.zeroc.com/ftp")
 7 print (r3)
 8 # 输出:ftp://www.zeroc.com/ftp
 9 # http://www.zeroc.com/www.zeroc.com/ftp
10 # http://www.zeroc.com/ftp
View Code

 13.2.4 URL的分析

1 r = urlsplit("http://www.python.org:80/faq.cgi?src=fie")
2 print (r)
View Code
1 r = urlsplit("http://www.python.org:80/faq.cgi?src=fie")
2 print (r)
3 
4 r = urlunsplit("http", "www.python.org", "faq", "", "")
5 print (r)
6 
7 r = urlunsplit(urlsplit("http://www.python.org/faq"))
8 print (r)
View Code
 1 from urllib.parse import urlparse,urljoin,urlunsplit
 2 abs_urls = ["http://www.python.org", "ftp://www.linux.org", "http://www.gtk.org", "file://"]
 3 rel_url = "faq.html"
 4 for abs_url in abs_urls:
 5     url = urljoin(abs_url, rel_url)
 6     expected_url = url
 7     scheme, netloc, path, query, fragment = urlsplit(url)
 8     if scheme or scheme == "file":
 9         print (url, "====> None")
10         continue
11     if scheme is not "ftp":
12         expected_url = urlunsplit(('http', netloc, path, query, fragment))
13     print (url, "====>", expected_url)
View Code

13.2.5 URL的编解码

表 13-3  URL编码中保留的字符
保留字符 URL 编码 保留字符 URL 编码 保留字符 URL 编码
%3B : %3A = %3D
/ %2F @ %40 & %26
%3F        

 

 

 

 

 

 

 

表 13-4  URL编码中不安全的字符
保留字符 URL 编码 保留字符 URL 编码 保留字符 URL 编码
< %3C { %7B   %7E
> %3E } %7D [ %5B
" %22 | %7C ] %5D
# %23 \ %5C ' %60
% %25 ^ %5E    

 

 

 

 

 

 

 

表 13-5  urllib.parse 模块中 URL 编解码方法
方法 功能 方法 功能
quote 对URL进行编码 unquote 对URL进行解码
quote_plus 同quote方法,进一步将空格表示成+符合 unquote_plus 同unquote方法,进一步将+符合变成空格

13.2.5 URL的编解码

 1 from urllib.parse import quote,quote_plus, unquote, unquote_plus
 2 r1 = quote("/-test")
 3 print (r1)
 4 r2 = quote("/-test/public html")
 5 print (r2)
 6 r3 = quote_plus("/-test/public html")
 7 print (r3)
 8 unquote_plus(r3)
 9 unquote_plus(r2)
10 r = quote("/-test", "-/")
11 # 输出:/-test
12 # /-test/public%20html
13 # %2F-test%2Fpublic+html
View Code

13.2.6 中文的编解码

1 quote("URL编码")
2 r = quote("URL编码")
3 unquote(r)
4 print (unquote(r))
View Code

13.2.7 查询参数的编码

 1 from urllib.parse import *
 2 urlencode([('keyword1', 'value1'), ('keyword2', 'value2')])
 3 urlencode()
 4 urlencode({'keyword1':'value1', 'keyword2':'value2'})
 5 a = {'keyword1':'value1', 'keyword2':'value2'}
 6 b = {'keyword2':'value2', 'keyword1':'value1'}
 7 a is b
 8 a == b
 9 urlencode([('keyword1',('value1', 'value2','value12') )])
10 urllib.urlencode([('keyword1',('value1', 'value2','value12') )], True)
11 r = urlencode([('keyword1',('value1', 'value2','value12') )])
12 unquote_plus(r)
View Code

13.3 CGL的使用

13.3.1 CGL介绍

表 13-6  HTTP支持的MIME类型
类型 子类型 说明 类型 子类型 说明
Application Octet-stream MIME编码的二进制数据 Multipart Mixed digest 混合类型,可以支持等操作文件上传
Audio Basic 音频文件 Text Html、Plain HTML文档,纯文本文档
Image Gif、Jpeg 图文文件 Video mpeg 视频文件
Message External-body 对信息的封装  
 1 print ("Content-Type:text/html")
 2 print ()
 3 print ("Content-Type:text/html\n\n")
 4 print ('<!DOCTYPE html">')
 5 print ('<html>')
 6 print ('<head>')
 7 print ('<br>&copy;2009')
 8 print ('</body>')
 9 print ('<html>')
10 # 输出:Content-Type:text/html
11 # 
12 # Content-Type:text/html
13 # 
14 # 
15 # <!DOCTYPE html">
16 # <html>
17 # <head>
18 # <br>&copy;2009
19 # </body>
20 # <html>
View Code

13.3.2 获取CGL环境信息

1 #!E:/Python3/python.exe
2 print ("Content-Type:text/html\n\n")
3 import datetime
4 print (datetime.datetime.now())
5 #输出:Content-Type:text/html
6 # 
7 # 
8 # 2019-01-06 18:00:37.908176
View Code
1 #!E:/Python3/python.exe
2 import os
3 print ("Content-Type:text/html\n\n")
4 remote_addr = os.environ['REMOTE_ADDR']
5 if remote_addr == '127.0.0.1':
6     print ("来自本地的访问")
7 else:
8     print ("来自外部的访问")
View Code
 1 #!E:/Python3/python.exe
 2 print ("Content-Type:text/html\n\n")
 3 import cgi
 4 cgi.print_environ()
 5 # 输出:Content-Type:text/html
 6 # 
 7 # 
 8 # 
 9 # <H3>Shell Environment:</H3>
10 # <DL>
11 # <DT> ALLUSERSPROFILE <DD> C:\ProgramData
12 # <DT> APPDATA <DD> C:\Users\刘永峰\AppData\Roaming
13 # <DT> COMMONPROGRAMFILES <DD> C:\Program Files\Common Files
14 # <DT> COMMONPROGRAMFILES(X86) <DD> C:\Program Files (x86)\Common Files
15 # <DT> COMMONPROGRAMW6432 <DD> C:\Program Files\Common Files
16 # <DT> COMPUTERNAME <DD> LAPTOP-PJ9B1NQR
17 # <DT> COMSPEC <DD> C:\windows\system32\cmd.exe
18 # <DT> CONFIGSETROOT <DD> C:\windows\ConfigSetRoot
19 # <DT> DRIVERDATA <DD> C:\Windows\System32\Drivers\DriverData
20 # <DT> FPS_BROWSER_APP_PROFILE_STRING <DD> Internet Explorer
21 # <DT> FPS_BROWSER_USER_PROFILE_STRING <DD> Default
22 # <DT> HOMEDRIVE <DD> C:
23 # <DT> HOMEPATH <DD> \Users\刘永峰
24 # <DT> LOCALAPPDATA <DD> C:\Users\刘永峰\AppData\Local
25 # <DT> LOGONSERVER <DD> \\LAPTOP-PJ9B1NQR
26 # <DT> NUMBER_OF_PROCESSORS <DD> 4
27 # <DT> ONEDRIVE <DD> C:\Users\刘永峰\OneDrive
28 # <DT> ONEDRIVECONSUMER <DD> C:\Users\刘永峰\OneDrive
29 # <DT> OS <DD> Windows_NT
30 # <DT> PATH <DD> F:\Anaconda3\Library\bin;F:\Anaconda3\DLLs;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;F:\Python35\Scripts;C:\Users\刘永峰\AppData\Local\Microsoft\WindowsApps;
31 # <DT> PATHEXT <DD> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
32 # <DT> PROCESSOR_ARCHITECTURE <DD> AMD64
33 # <DT> PROCESSOR_IDENTIFIER <DD> Intel64 Family 6 Model 92 Stepping 9, GenuineIntel
34 # <DT> PROCESSOR_LEVEL <DD> 6
35 # <DT> PROCESSOR_REVISION <DD> 5c09
36 # <DT> PROGRAMDATA <DD> C:\ProgramData
37 # <DT> PROGRAMFILES <DD> C:\Program Files
38 # <DT> PROGRAMFILES(X86) <DD> C:\Program Files (x86)
39 # <DT> PROGRAMW6432 <DD> C:\Program Files
40 # <DT> PSMODULEPATH <DD> C:\Program Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules
41 # <DT> PUBLIC <DD> C:\Users\Public
42 # <DT> PYCHARM_HOSTED <DD> 1
43 # <DT> PYCHARM_MATPLOTLIB_PORT <DD> 50202
44 # <DT> PYTHONIOENCODING <DD> UTF-8
45 # <DT> PYTHONPATH <DD> C:\PyCharm 2017.3.3\helpers\pycharm_matplotlib_backend;D:\Python\零基础学 python
46 # <DT> PYTHONUNBUFFERED <DD> 1
47 # <DT> SESSIONNAME <DD> Console
48 # <DT> SYSTEMDRIVE <DD> C:
49 # <DT> SYSTEMROOT <DD> C:\windows
50 # <DT> TEMP <DD> C:\Users\刘永峰\AppData\Local\Temp
51 # <DT> TMP <DD> C:\Users\刘永峰\AppData\Local\Temp
52 # <DT> USERDOMAIN <DD> LAPTOP-PJ9B1NQR
53 # <DT> USERDOMAIN_ROAMINGPROFILE <DD> LAPTOP-PJ9B1NQR
54 # <DT> USERNAME <DD> 刘永峰
55 # <DT> USERPROFILE <DD> C:\Users\刘永峰
56 # <DT> WINDIR <DD> C:\windows
57 # </DL>
View Code
表 13-7  CGL 中的部分环境变量
环境变量 含   义 环境变量 含   义
REMOTE_ADDR 服务器的地址 SCRIPT_NAME 服务器执行脚本名字
PATH_INFO 路径信息 HTTP_USER_AGENT 客户端软件
REQUEST_METHOD 客户端请求的方法,可以为GET或POST SERVER_PROTOCOL 服务器支持协议
SERVER_NAME 服务器名字 SERVER_SOFTWARE 服务器端软件信息
SERVER_PORT 服务器端口 QUERY_STRING 查询字符串

13.3.3 解析用户的输入

1 #!E:/Python3/python.exe
2 print ("Content-Type:text/html\n\n")
3 import cgi
4 form = cgi.FieldStorage()
5 for key in form.keys():
6     print (key, "==>", form[key].value)
7     print ("<br/>")
View Code
1 #!E:/Python3/python.exe
2 print ("Content-Type:text/html\n\n")
3 import cgi
4 form = cgi.FieldStorage()
5 for key in form.keys():
6     print (key, "==>", form.getlist(key))
7     print ("<br/>")
View Code
1 #!E:/Python3/python.exe
2 print ("Content-Type:text/html\n\n")
3 import cgi
4 form = cgi.FieldStorage()
5 for key in form.keys():
6     for value in form.getlist(key):
7         print (key, "==>", cgi.escape(value))
8         print ("<br/>")
View Code

13.4 获取HTML资源

13.4.1 使用urlopen和urlretrieve获取HTTP资源

 1 from urllib.request import urlopen
 2 r = urlopen("http://www.python.org")
 3 print (r.read())
 4 # 输出:b'<!doctype html>\n<!--[if lt IE 7]>  
 5 #  <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9">  
 6 #  <![endif]-->\n<!--[if IE 7]>     
 7 #  <html class="no-js ie7 lt-ie8 lt-ie9">         
 8 #  <![endif]-->\n<!--[if IE 8]>      
 9 # <html class="no-js ie8 lt-ie9">               
10 #   <![endif]-->\n<!--[if gt IE 8]><!--><html class="no-js" lang="en" dir="ltr">
11 #   <!--<![endif]-->\n\n<head>\n    
12 # <meta charset="utf-8">\n    
13 # <meta http-equiv="X-UA-Compatible" content="IE=edge">\n\n    
14 # <link rel="prefetch" href="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">\n\n  
15 #   <meta name="application-name" content="Python.org">\n    
16 # <meta name="msapplication-tooltip" content="The official home of the 
17 # Python Programming Language">\n    <meta name="apple-mobile-web-app-title"
18 #  content="Python.org">\n    <meta name="apple-mobile-web-app-capable" content="yes">\n  
19 #   <meta name="apple-mobile-web-app-status-bar-style" content="black">\n\n   
20 #  <meta name="viewport" content="width=device-width, initial-scale=1.0">\n   
21 #  <meta name="HandheldFriendly" content="True">\n    
22 # <meta name="format-detection" content="telephone=no">\n    
23 # <meta http-equiv="cleartype" content="on">\n    
24 # <meta http-equiv="imagetoolbar" content="false">\n\n    
25 # <script src="/static/js/libs/modernizr.js"></script>\n\n  
26 #   <link href="/static/stylesheets/style.css" rel="stylesheet" 
27 # type="text/css" title="default" />\n   
28 #  <link href="/static/stylesheets/mq.css" rel="stylesheet" 
29 # type="text/css" media="not print, braille, embossed, speech, tty" />\n   
30 #  \n\n    <!--[if (lte IE 8)&(!IEMobile)]>\n   
31 #  <link href="/static/stylesheets/no-mq.css"
32 #  rel="stylesheet" type="text/css" media="screen" />\n    \n  
33 #   \n    <![endif]-->\n\n    \n    
34 # <link rel="icon" type="image/x-icon" href="/static/favicon.ico">\n    
35 # <link rel="apple-touch-icon-precomposed" sizes="144x144" 
36 # href="/static/apple-touch-icon-144x144-precomposed.png">\n    
37 # <link rel="apple-touch-icon-precomposed" sizes="114x114"
38 #  href="/static/apple-touch-icon-114x114-precomposed.png">\n   
39 #  <link rel="apple-touch-icon-precomposed" 
40 # sizes="72x72" href="/static/apple-touch-icon-72x72-precomposed.png">\n   
41 #  <link rel="apple-touch-icon-precomposed" href="/static/apple-touch-icon-precomposed.png">\n 
42 #    <link rel="apple-touch-icon" href="/static/apple-touch-icon-precomposed.png">\n\n 
43 #    \n    <meta name="msapplication-TileImage"
44 #  content="/static/metro-icon-144x144-precomposed.png"><!-- white shape -->\n   
45 #  <meta name="msapplication-TileColor" content="#3673a5"><!-- python blue -->\n   
46 #  <meta name="msapplication-navbutton-color" content="#3673a5">\n\n   
47 #  <title>Welcome to Python.org</title>\n\n   
48 #  <meta name="description" content="The official home of the Python Programming Language">\n 
49 #    <meta name="keywords" content="Python programming language object
50 #  oriented web free open source software license documentation download community">\n\n  
51 #   \n    <meta property="og:type" content="website">\n   
52 #  <meta property="og:site_name" content="Python.org">\n    
53 # <meta property="og:title" content="Welcome to Python.org">\n  
54 #   <meta property="og:description" content="The official home of the Python 
55 # Programming Language">\n    \n    <meta property="og:image" 
56 # content="https://www.python.org/static/opengraph-icon-200x200.png">\n   
57 #  <meta property="og:image:secure_url" 
58 # content="https://www.python.org/static/opengraph-icon-200x200.png">\n    \n    
59 # <meta property="og:url" content="https://www.python.org/">\n\n    
60 # <link rel="author" href="/static/humans.txt">\n\n    
61 # <link rel="alternate" type="application/rss+xml" title="Python Enhancement Proposals"\n  
62 # href="https://www.python.org/dev/peps/peps.rss/">\n  
63 #  <link rel="alternate" type="application/rss+xml" title="Python Job Opportunities"\n          
64 # href="https://www.python.org/jobs/feed/rss/">\n    
65 # <link rel="alternate" type="application/rss+xml" title="Python Software Foundation News"\n  
66 #         href="https://feeds.feedburner.com/PythonSoftwareFoundationNews">\n    
67 # <link rel="alternate" type="application/rss+xml" title="Python Insider"\n          
68 # href="https://feeds.feedburner.com/PythonInsider">\n\n    \n\n    \n    
69 # <script type="application/ld+json">\n     {\n       "@context": "http://schema.org",\n       
70 # "@type": "WebSite",\n       "url": "https://www.python.org/",\n       
71 # "potentialAction": {\n         "@type": "SearchAction",\n         "target": "https://www.python.org/search/?q={search_term_string}",\n         "query-input": "required name=search_term_string"\n       }\n     }\n    </script>\n\n    \n    <script type="text/javascript">\n    var _gaq = _gaq || [];\n    _gaq.push([\'_setAccount\', \'UA-39055973-1\']);\n    _gaq.push([\'_trackPageview\']);\n\n    (function() {\n        var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;\n        ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';\n        var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);\n    })();\n    </script>\n    \n</head>\n\n<body class="python home" id="homepage">\n\n    <div id="touchnav-wrapper">\n\n        <div id="nojs" class="do-not-print">\n            <p><strong>Notice:</strong> While Javascript is not essential for this website, your interaction with the content will be limited. Please turn Javascript on for the full experience. </p>\n        </div>\n\n        <!--[if lt IE 8]>\n        <div id="oldie-warning" class="do-not-print">\n            <p><strong>Notice:</strong> Your browser is <em>ancient</em> and <a href="http://www.ie6countdown.com/">Microsoft agrees</a>. <a href="http://browsehappy.com/">Upgrade to a different browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to experience a better web.</p>\n        </div>\n        <![endif]-->\n\n        <!-- Sister Site Links -->\n        <div id="top" class="top-bar do-not-print">\n\n            <nav class="meta-navigation container" role="navigation">\n\n                \n                <div class="skip-link screen-reader-text">\n                    <a href="#content" title="Skip to content">Skip to content</a>\n                </div>\n\n                \n                <a id="close-python-network" class="jump-link" href="#python-network" aria-hidden="true">\n                    <span aria-hidden="true" class="icon-arrow-down"><span>&#9660;</span></span> Close\n                </a>\n\n                \n\n<ul class="menu" role="tree">\n    \n    <li class="python-meta current_item selectedcurrent_branch selected">\n        <a href="/" title="The Python Programming Language" class="current_item selectedcurrent_branch selected">Python</a>\n    </li>\n    \n    <li class="psf-meta ">\n        <a href="/psf-landing/" title="The Python Software Foundation" >PSF</a>\n    </li>\n    \n    <li class="docs-meta ">\n        <a href="https://docs.python.org" title="Python Documentation" >Docs</a>\n    </li>\n    \n    <li class="pypi-meta ">\n        <a href="https://pypi.python.org/" title="Python Package Index" >PyPI</a>\n    </li>\n    \n    <li class="jobs-meta ">\n        <a href="/jobs/" title="Python Job Board" >Jobs</a>\n    </li>\n    \n    <li class="shop-meta ">\n        <a href="/community/" title="Python Community" >Community</a>\n    </li>\n    \n</ul>\n\n\n                <a id="python-network" class="jump-link" href="#top" aria-hidden="true">\n                    <span aria-hidden="true" class="icon-arrow-up"><span>&#9650;</span></span> The Python Network\n                </a>\n\n            </nav>\n\n        </div>\n\n        <!-- Header elements -->\n        <header class="main-header" role="banner">\n            <div class="container">\n\n                <h1 class="site-headline">\n                    <a href="/"><img class="python-logo" src="/static/img/python-logo.png" alt="python&trade;"></a>\n                </h1>\n\n                <div class="options-bar do-not-print">\n\n                    \n                    <a id="site-map-link" class="jump-to-menu" href="#site-map"><span class="menu-icon">&equiv;</span> Menu</a><form class="search-the-site" action="/search/" method="get">\n                        <fieldset title="Search Python.org">\n\n                            <span aria-hidden="true" class="icon-search"></span>\n\n                            <label class="screen-reader-text" for="id-search-field">Search This Site</label>\n                            <input id="id-search-field" name="q" type="search" role="textbox" class="search-field" placeholder="Search" value="" tabindex="1">\n\n                            <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">\n                                GO\n                            </button>\n\n                            \n                            <!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" tabindex="4"><![endif]-->\n\n                        </fieldset>\n                    </form><span class="breaker"></span><div class="adjust-font-size" aria-hidden="true">\n                        <ul class="navigation menu" aria-label="Adjust Text Size on Page">\n                            <li class="tier-1 last" aria-haspopup="true">\n                                <a href="#" class="action-trigger"><strong><small>A</small> A</strong></a>\n                                <ul class="subnav menu">\n                                    <li class="tier-2 element-1" role="treeitem"><a class="text-shrink" title="Make Text Smaller" href="javascript:;">Smaller</a></li>\n                                    <li class="tier-2 element-2" role="treeitem"><a class="text-grow" title="Make Text Larger" href="javascript:;">Larger</a></li>\n                                    <li class="tier-2 element-3" role="treeitem"><a class="text-reset" title="Reset any font size changes I have made" href="javascript:;">Reset</a></li>\n                                </ul>\n                            </li>\n                        </ul>\n                    </div><div class="winkwink-nudgenudge">\n                        <ul class="navigation menu" aria-label="Social Media Navigation">\n                            <li class="tier-1 last" aria-haspopup="true">\n                                <a href="#" class="action-trigger">Socialize</a>\n                                <ul class="subnav menu">\n                                    <li class="tier-2 element-1" role="treeitem"><a href="http://plus.google.com/+Python"><span aria-hidden="true" class="icon-google-plus"></span>Google+</a></li>\n                                    <li class="tier-2 element-2" role="treeitem"><a href="http://www.facebook.com/pythonlang?fref=ts"><span aria-hidden="true" class="icon-facebook"></span>Facebook</a></li>\n                                    <li class="tier-2 element-3" role="treeitem"><a href="http://twitter.com/ThePSF"><span aria-hidden="true" class="icon-twitter"></span>Twitter</a></li>\n                                    <li class="tier-2 element-4" role="treeitem"><a href="/community/irc/"><span aria-hidden="true" class="icon-freenode"></span>Chat on IRC</a></li>\n                                </ul>\n                            </li>\n                        </ul>\n                    </div><div class="account-signin">\n                        <ul class="navigation menu" aria-label="Social Media Navigation">\n                            <li class="tier-1 last" aria-haspopup="true">\n                                \n                                <a href="/accounts/login/" title="Sign Up or Sign In to Python.org">Sign In</a>\n                                <ul class="subnav menu">\n                                    <li class="tier-2 element-1" role="treeitem"><a href="/accounts/signup/">Sign Up / Register</a></li>\n                                    <li class="tier-2 element-2" role="treeitem"><a href="/accounts/login/">Sign In</a></li>\n                                </ul>\n                                \n                            </li>\n                        </ul>\n                    </div>\n\n                </div><!-- end options-bar -->\n\n                <nav id="mainnav" class="python-navigation main-navigation do-not-print" role="navigation">\n                    \n                        \n<ul class="navigation menu" role="menubar" aria-label="Main Navigation">\n  \n    \n    \n    <li id="about" class="tier-1 element-1  " aria-haspopup="true">\n        <a href="/about/" title="" class="">About</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/about/apps/" title="">Applications</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/about/quotes/" title="">Quotes</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/about/gettingstarted/" title="">Getting Started</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/about/help/" title="">Help</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="http://brochure.getpython.info/" title="">Python Brochure</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    <li id="downloads" class="tier-1 element-2  " aria-haspopup="true">\n        <a href="/downloads/" title="" class="">Downloads</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/downloads/" title="">All releases</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/downloads/source/" title="">Source code</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/downloads/windows/" title="">Windows</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/downloads/mac-osx/" title="">Mac OS X</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="/download/other/" title="">Other Platforms</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="https://docs.python.org/3/license.html" title="">License</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="/download/alternatives" title="">Alternative Implementations</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    <li id="documentation" class="tier-1 element-3  " aria-haspopup="true">\n        <a href="/doc/" title="" class="">Documentation</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/doc/" title="">Docs</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/doc/av" title="">Audio/Visual Talks</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="https://wiki.python.org/moin/BeginnersGuide" title="">Beginner&#39;s Guide</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="https://devguide.python.org/" title="">Developer&#39;s Guide</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="https://docs.python.org/faq/" title="">FAQ</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="http://wiki.python.org/moin/Languages" title="">Non-English Docs</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="http://python.org/dev/peps/" title="">PEP Index</a></li>\n    \n        <li class="tier-2 element-8" role="treeitem"><a href="https://wiki.python.org/moin/PythonBooks" title="">Python Books</a></li>\n    \n        <li class="tier-2 element-9" role="treeitem"><a href="/doc/essays/" title="">Python Essays</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    <li id="community" class="tier-1 element-4  " aria-haspopup="true">\n        <a href="/community/" title="" class="">Community</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/community/survey" title="">Community Survey</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/community/diversity/" title="">Diversity</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/community/lists/" title="">Mailing Lists</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/community/irc/" title="">IRC</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="/community/forums/" title="">Forums</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="/community/workshops/" title="">Python Conferences</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="/community/sigs/" title="">Special Interest Groups</a></li>\n    \n        <li class="tier-2 element-8" role="treeitem"><a href="/community/logos/" title="">Python Logo</a></li>\n    \n        <li class="tier-2 element-9" role="treeitem"><a href="https://wiki.python.org/moin/" title="">Python Wiki</a></li>\n    \n        <li class="tier-2 element-10" role="treeitem"><a href="/community/merchandise/" title="">Merchandise</a></li>\n    \n        <li class="tier-2 element-11" role="treeitem"><a href="/community/awards" title="">Community Awards</a></li>\n    \n        <li class="tier-2 element-12" role="treeitem"><a href="https://www.python.org/psf/codeofconduct/" title="">Code of Conduct</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    <li id="success-stories" class="tier-1 element-5  " aria-haspopup="true">\n        <a href="/success-stories/" title="success-stories" class="">Success Stories</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/success-stories/category/arts/" title="">Arts</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/success-stories/category/business/" title="">Business</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/success-stories/category/education/" title="">Education</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/success-stories/category/engineering/" title="">Engineering</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="/success-stories/category/government/" title="">Government</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="/success-stories/category/scientific/" title="">Scientific</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="/success-stories/category/software-development/" title="">Software Development</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    <li id="news" class="tier-1 element-6  " aria-haspopup="true">\n        <a href="/blogs/" title="News from around the Python world" class="">News</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/blogs/" title="Python Insider Blog Posts">Python News</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="http://planetpython.org/" title="Planet Python">Community News</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="http://pyfound.blogspot.com/" title="PSF Blog">PSF News</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="http://pycon.blogspot.com/" title="PyCon Blog">PyCon News</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    <li id="events" class="tier-1 element-7  " aria-haspopup="true">\n        <a href="/events/" title="" class="">Events</a>\n        \n            \n\n<ul class="subnav menu" role="menu" aria-hidden="true">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/events/python-events" title="">Python Events</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/events/python-user-group/" title="">User Group Events</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/events/python-events/past/" title="">Python Events Archive</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/events/python-user-group/past/" title="">User Group Events Archive</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event" title="">Submit an Event</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    \n    \n    \n  \n</ul>\n\n                    \n                </nav>\n\n                <div class="header-banner "> <!-- for optional "do-not-print" class -->\n                    \n        <div id="dive-into-python" class="flex-slideshow slideshow">\n\n            <ul class="launch-shell menu" id="launch-shell">\n                <li>\n                    <a class="button prompt" id="start-shell" data-shell-container="#dive-into-python" href="/shell/">&gt;_\n                        <span class="message">Launch Interactive Shell</span>\n                    </a>\n                </li>\n            </ul>\n\n            <ul class="slides menu">\n                \n                <li>\n                    <div class="slide-code"><pre><code><span class="comment"># Python 3: Fibonacci series up to n</span>\r\n>>> def fib(n):\r\n>>>     a, b = 0, 1\r\n>>>     while a &lt; n:\r\n>>>         print(a, end=\' \')\r\n>>>         a, b = b, a+b\r\n>>>     print()\r\n>>> fib(1000)\r\n<span class="output">0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</span></code></pre></div>\n                    <div class="slide-copy"><h1>Functions Defined</h1>\r\n<p>The core of extensible programming is defining functions. Python allows mandatory and optional arguments, keyword arguments, and even arbitrary argument lists. <a href="//docs.python.org/3/tutorial/controlflow.html#defining-functions">More about defining functions in Python&nbsp;3</a></p></div>\n                </li>\n                \n                <li>\n                    <div class="slide-code"><pre><code><span class="comment"># Python 3: List comprehensions</span>\r\n>>> fruits = [\'Banana\', \'Apple\', \'Lime\']\r\n>>> loud_fruits = [fruit.upper() for fruit in fruits]\r\n>>> print(loud_fruits)\r\n<span class="output">[\'BANANA\', \'APPLE\', \'LIME\']</span>\r\n\r\n<span class="comment"># List and the enumerate function</span>\r\n>>> list(enumerate(fruits))\r\n<span class="output">[(0, \'Banana\'), (1, \'Apple\'), (2, \'Lime\')]</span></code></pre></div>\n                    <div class="slide-copy"><h1>Compound Data Types</h1>\r\n<p>Lists (known as arrays in other languages) are one of the compound data types that Python understands. Lists can be indexed, sliced and manipulated with other built-in functions. <a href="//docs.python.org/3/tutorial/introduction.html#lists">More about lists in Python&nbsp;3</a></p></div>\n                </li>\n                \n                <li>\n                    <div class="slide-code"><pre><code><span class="comment"># Python 3: Simple arithmetic</span>\r\n>>> 1 / 2\r\n<span class="output">0.5</span>\r\n>>> 2 ** 3\r\n<span class="output">8</span>\r\n>>> 17 / 3  <span class="comment"># classic division returns a float</span>\r\n<span class="output">5.666666666666667</span>\r\n>>> 17 // 3  <span class="comment"># floor division</span>\r\n<span class="output">5</span></code></pre></div>\n                    <div class="slide-copy"><h1>Intuitive Interpretation</h1>\r\n<p>Calculations are simple with Python, and expression syntax is straightforward: the operators <code>+</code>, <code>-</code>, <code>*</code> and <code>/</code> work as expected; parentheses <code>()</code> can be used for grouping. <a href="http://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator">More about simple math functions in Python&nbsp;3</a>.</p></div>\n                </li>\n                \n                <li>\n                    <div class="slide-code"><pre><code><span class="comment"># Python 3: Simple output (with Unicode)</span>\r\n>>> print("Hello, I\'m Python!")\r\n<span class="output">Hello, I\'m Python!</span>\r\n\r\n<span class="comment"># Input, assignment</span>\r\n>>> name = input(\'What is your name?\\n\')\r\n>>> print(\'Hi, %s.\' % name)\r\n<span class="output">What is your name?\r\nPython\r\nHi, Python.</span></code></pre></div>\n                    <div class="slide-copy"><h1>Quick &amp; Easy to Learn</h1>\r\n<p>Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn. <a href="//docs.python.org/3/tutorial/">Whet your appetite</a> with our Python&nbsp;3 overview.</p>\r\n                   </div>\n                </li>\n                \n                <li>\n                    <div class="slide-code"><pre><code><span class="comment"># For loop on a list</span>\r\n>>> numbers = [2, 4, 6, 8]\r\n>>> product = 1\r\n>>> for number in numbers:\r\n...    product = product * number\r\n... \r\n>>> print(\'The product is:\', product)\r\n<span class="output">The product is: 384</span></code></pre></div>\n                    <div class="slide-copy"><h1>All the Flow You&rsquo;d Expect</h1>\r\n<p>Python knows the usual control flow statements that other languages speak &mdash; <code>if</code>, <code>for</code>, <code>while</code> and <code>range</code> &mdash; with some of its own twists, of course. <a href="//docs.python.org/3/tutorial/controlflow.html">More control flow tools in Python&nbsp;3</a></p></div>\n                </li>\n                \n            </ul>\n        </div>\n\n\n                </div>\n\n                \n        <div class="introduction">\n            <p>Python is a programming language that lets you work quickly <span class="breaker"></span>and integrate systems more effectively. <a class="readmore" href="/doc/">Learn More</a></p>\n        </div>\n\n\n             </div><!-- end .container -->\n        </header>\n\n        <div id="content" class="content-wrapper">\n            <!-- Main Content Column -->\n            <div class="container">\n\n                <section class="main-content " role="main">\n\n                    \n                    \n\n                    \n\n                    \n\n                <div class="row">\n\n                    <div class="small-widget get-started-widget">\n                        <h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Get Started</h2>\r\n<p>Whether you\'re new to programming or an experienced developer, it\'s easy to learn and use Python.</p>\r\n<p><a href="/about/gettingstarted/">Start with our Beginner&rsquo;s Guide</a></p>\n                    </div>\n\n                    <div class="small-widget download-widget">\n                        <h2 class="widget-title"><span aria-hidden="true" class="icon-download"></span>Download</h2>\n<p>Python source code and installers are available for download for all versions!</p>\n<p>Latest: <a href="/downloads/release/python-372/">Python 3.7.2</a></p>\n                    </div>\n\n                    <div class="small-widget documentation-widget">\n                        <h2 class="widget-title"><span aria-hidden="true" class="icon-documentation"></span>Docs</h2>\r\n<p>Documentation for Python\'s standard library, along with tutorials and guides, are available online.</p>\r\n<p><a href="https://docs.python.org">docs.python.org</a></p>\n                    </div>\n\n                    <div class="small-widget jobs-widget last">\n                        <h2 class="widget-title"><span aria-hidden="true" class="icon-jobs"></span>Jobs</h2>\r\n<p>Looking for work or have a Python related position that you\'re trying to hire for? Our <strong>relaunched community-run job board</strong> is the place to go.</p>\r\n<p><a href="//jobs.python.org">jobs.python.org</a></p>\n                    </div>\n\n                </div>\n\n                <div class="list-widgets row">\n\n                    <div class="medium-widget blog-widget">\n                        \n                        <div class="shrubbery">\n                        \n                            <h2 class="widget-title"><span aria-hidden="true" class="icon-news"></span>Latest News</h2>\n                            <p class="give-me-more"><a href="http://blog.python.org" title="More News">More</a></p>\n                            \n                            <ul class="menu">\n                                \n                                \n                                <li>\n<time datetime="2018-12-24T11:26:00+00:00"><span class="say-no-more">2018-</span>12-24</time>\n <a href="http://feedproxy.google.com/~r/PythonInsider/~3/KoVsY316T7U/python-372-and-368-are-now-available.html">Python&nbsp;3.7.2&nbsp;and&nbsp;3.6.8&nbsp;are now available. &nbsp;Python 3.7.2 is the next maintenance release ...</a></li>\n                                \n                                <li>\n<time datetime="2018-12-24T11:26:00+00:00"><span class="say-no-more">2018-</span>12-24</time>\n <a href="http://feedproxy.google.com/~r/PythonInsider/~3/o5ROzg9M8NY/python-372-and-368-are-now-available.html">Python&nbsp;3.7.2&nbsp;and&nbsp;3.6.8&nbsp;are now available. &nbsp;Python 3.7.2 is the next maintenance release ...</a></li>\n                                \n                                <li>\n<time datetime="2018-12-20T17:55:00.000003+00:00"><span class="say-no-more">2018-</span>12-20</time>\n <a href="http://feedproxy.google.com/~r/PythonSoftwareFoundationNews/~3/UJLhXUl_Le8/upcoming-pypi-improvements-for-2019.html">The Python Package Index (PyPI) is far and away the ...</a></li>\n                                \n                                <li>\n<time datetime="2018-12-19T19:11:00.000002+00:00"><span class="say-no-more">2018-</span>12-19</time>\n <a href="http://feedproxy.google.com/~r/PythonSoftwareFoundationNews/~3/HPqp93-tF3M/pypi-security-and-accessibility-q1-2019.html">Earlier this year we launched a Request for Information (RFI) ...</a></li>\n                                \n                                <li>\n<time datetime="2018-12-17T11:30:00+00:00"><span class="say-no-more">2018-</span>12-17</time>\n <a href="http://feedproxy.google.com/~r/PythonSoftwareFoundationNews/~3/oAgPaokjjg4/evangelizing-python-in-africa-chukwudi_65.html">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px \'Helvetica Neue\'; ...</a></li>\n                                \n                            </ul>\n                        </div><!-- end .shrubbery -->\n\n                    </div>\n\n                    <div class="medium-widget event-widget last">\n                        \n                        <div class="shrubbery">\n                        \n                            <h2 class="widget-title"><span aria-hidden="true" class="icon-calendar"></span>Upcoming Events</h2>\n                            <p class="give-me-more"><a href="/events/calendars/" title="More Events">More</a></p>\n                            \n                            <ul class="menu">\n                                \n                                \n                                \n                                <li>\n<time datetime="2019-02-02T00:00:00+00:00"><span class="say-no-more">2019-</span>02-02</time>\n <a href="/events/python-events/800/">FOSDEM 2019</a></li>\n                                \n                                \n                                \n                                <li>\n<time datetime="2019-02-09T00:00:00+00:00"><span class="say-no-more">2019-</span>02-09</time>\n <a href="/events/python-user-group/803/">DjangoGirls Bangalore</a></li>\n                                \n                                \n                                \n                                <li>\n<time datetime="2019-02-09T00:00:00+00:00"><span class="say-no-more">2019-</span>02-09</time>\n <a href="/events/python-events/770/">PyTennessee 2019</a></li>\n                                \n                                \n                                \n                                <li>\n<time datetime="2019-02-15T00:00:00+00:00"><span class="say-no-more">2019-</span>02-15</time>\n <a href="/events/python-events/801/">PyCon Belarus 2019</a></li>\n                                \n                                \n                                \n                                <li>\n<time datetime="2019-02-19T00:00:00+00:00"><span class="say-no-more">2019-</span>02-19</time>\n <a href="/events/python-events/790/">PyCon Namibia 2019</a></li>\n                                \n                                \n                            </ul>\n                        </div>\n\n                    </div>\n\n                </div>\n\n                <div class="row">\n\n                    <div class="medium-widget success-stories-widget">\n                        \n\n\n\n                        <div class="shrubbery">\n                            \n\n                            <h2 class="widget-title"><span aria-hidden="true" class="icon-success-stories"></span>Success Stories</h2>\n                            <p class="give-me-more"><a href="/success-stories/" title="More Success Stories">More</a></p>\n\n                            \n                            <div class="success-story-item" id="success-story-833">\n\n                            <blockquote>\n                                <a href="/success-stories/using-python-to-automate-tedious-tasks/">How we used Python to automate a problem that occurred infrequently, but was a huge nuisance when it did occur.</a>\n                            </blockquote>\n\n                            <table cellpadding="0" cellspacing="0" border="0" width="100%" class="quote-from">\n                                <tbody>\n                                    <tr>\n                                        \n                                        <td><p><a href="/success-stories/using-python-to-automate-tedious-tasks/">Using Python to Automate Tedious Tasks</a> <em>by Nat Dunn</em></p></td>\n                                    </tr>\n                                </tbody>\n                            </table>\n                            </div>\n                            \n\n                        </div><!-- end .shrubbery -->\n\n                    </div>\n\n                    <div class="medium-widget applications-widget last">\n                        <div class="shrubbery">\n                            <h2 class="widget-title"><span aria-hidden="true" class="icon-python"></span>Use Python for&hellip;</h2>\r\n<p class="give-me-more"><a href="/about/apps" title="More Applications">More</a></p>\r\n\r\n<ul class="menu">\r\n    <li><b>Web Development</b>:\r\n        <span class="tag-wrapper"><a class="tag" href="http://www.djangoproject.com/">Django</a>, <a class="tag" href="http://www.pylonsproject.org/">Pyramid</a>, <a class="tag" href="http://bottlepy.org">Bottle</a>, <a class="tag" href="http://tornadoweb.org">Tornado</a>, <a href="http://flask.pocoo.org/" class="tag">Flask</a>, <a class="tag" href="http://www.web2py.com/">web2py</a></span></li>\r\n    <li><b>GUI Development</b>:\r\n        <span class="tag-wrapper"><a class="tag" href="http://wiki.python.org/moin/TkInter">tkInter</a>, <a class="tag" href="https://wiki.gnome.org/Projects/PyGObject">PyGObject</a>, <a class="tag" href="http://www.riverbankcomputing.co.uk/software/pyqt/intro">PyQt</a>, <a class="tag" href="https://wiki.qt.io/PySide">PySide</a>, <a class="tag" href="https://kivy.org/">Kivy</a>, <a class="tag" href="http://www.wxpython.org/">wxPython</a></span></li>\r\n    <li><b>Scientific and Numeric</b>:\r\n        <span class="tag-wrapper">\r\n<a class="tag" href="http://www.scipy.org">SciPy</a>, <a class="tag" href="http://pandas.pydata.org/">Pandas</a>, <a href="http://ipython.org" class="tag">IPython</a></span></li>\r\n    <li><b>Software Development</b>:\r\n        <span class="tag-wrapper"><a class="tag" href="http://buildbot.net/">Buildbot</a>, <a class="tag" href="http://trac.edgewall.org/">Trac</a>, <a class="tag" href="http://roundup.sourceforge.net/">Roundup</a></span></li>\r\n    <li><b>System Administration</b>:\r\n        <span class="tag-wrapper"><a class="tag" href="http://www.ansible.com">Ansible</a>, <a class="tag" href="http://www.saltstack.com">Salt</a>, <a class="tag" href="https://www.openstack.org">OpenStack</a></span></li>\r\n</ul>\r\n\n                        </div><!-- end .shrubbery -->\n                    </div>\n\n                </div>\n\n                \n                <div class="pep-widget">\n\n                    <h2 class="widget-title">\n                        <span class="prompt">&gt;&gt;&gt;</span> <a href="/dev/peps/">Python Enhancement Proposals<span class="say-no-more"> (PEPs)</span></a>: The future of Python<span class="say-no-more"> is discussed here.</span>\n                        <a aria-hidden="true" class="rss-link" href="/dev/peps/peps.rss"><span class="icon-feed"></span> RSS</a>\n                    </h2>\n\n\n                    \n                    \n                </div>\n\n                                <div class="psf-widget">\n\n                    <div class="python-logo"></div>\n                    \n                    <h2 class="widget-title">\r\n    <span class="prompt">&gt;&gt;&gt;</span> <a href="/psf/">Python Software Foundation</a>\r\n</h2>\r\n<p>The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. <a class="readmore" href="/psf/">Learn more</a> </p>\r\n<p class="click-these">\r\n    <a class="button" href="/users/membership/">Become a Member</a>\r\n    <a class="button" href="/psf/donations/">Donate to the PSF</a>\r\n</p>\n                </div>\n\n\n\n\n                </section>\n\n                \n                \n\n                \n                \n\n\n            </div><!-- end .container -->\n        </div><!-- end #content .content-wrapper -->\n\n        <!-- Footer and social media list -->\n        <footer id="site-map" class="main-footer" role="contentinfo">\n            <div class="main-footer-links">\n                <div class="container">\n\n                    \n                    <a id="back-to-top-1" class="jump-link" href="#python-network"><span aria-hidden="true" class="icon-arrow-up"><span>&#9650;</span></span> Back to Top</a>\n\n                    \n\n<ul class="sitemap navigation menu do-not-print" role="tree" id="container">\n    \n    <li class="tier-1 element-1">\n        <a href="/about/" >About</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/about/apps/" title="">Applications</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/about/quotes/" title="">Quotes</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/about/gettingstarted/" title="">Getting Started</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/about/help/" title="">Help</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="http://brochure.getpython.info/" title="">Python Brochure</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-2">\n        <a href="/downloads/" >Downloads</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/downloads/" title="">All releases</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/downloads/source/" title="">Source code</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/downloads/windows/" title="">Windows</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/downloads/mac-osx/" title="">Mac OS X</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="/download/other/" title="">Other Platforms</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="https://docs.python.org/3/license.html" title="">License</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="/download/alternatives" title="">Alternative Implementations</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-3">\n        <a href="/doc/" >Documentation</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/doc/" title="">Docs</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/doc/av" title="">Audio/Visual Talks</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="https://wiki.python.org/moin/BeginnersGuide" title="">Beginner&#39;s Guide</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="https://devguide.python.org/" title="">Developer&#39;s Guide</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="https://docs.python.org/faq/" title="">FAQ</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="http://wiki.python.org/moin/Languages" title="">Non-English Docs</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="http://python.org/dev/peps/" title="">PEP Index</a></li>\n    \n        <li class="tier-2 element-8" role="treeitem"><a href="https://wiki.python.org/moin/PythonBooks" title="">Python Books</a></li>\n    \n        <li class="tier-2 element-9" role="treeitem"><a href="/doc/essays/" title="">Python Essays</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-4">\n        <a href="/community/" >Community</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/community/survey" title="">Community Survey</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/community/diversity/" title="">Diversity</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/community/lists/" title="">Mailing Lists</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/community/irc/" title="">IRC</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="/community/forums/" title="">Forums</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="/community/workshops/" title="">Python Conferences</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="/community/sigs/" title="">Special Interest Groups</a></li>\n    \n        <li class="tier-2 element-8" role="treeitem"><a href="/community/logos/" title="">Python Logo</a></li>\n    \n        <li class="tier-2 element-9" role="treeitem"><a href="https://wiki.python.org/moin/" title="">Python Wiki</a></li>\n    \n        <li class="tier-2 element-10" role="treeitem"><a href="/community/merchandise/" title="">Merchandise</a></li>\n    \n        <li class="tier-2 element-11" role="treeitem"><a href="/community/awards" title="">Community Awards</a></li>\n    \n        <li class="tier-2 element-12" role="treeitem"><a href="https://www.python.org/psf/codeofconduct/" title="">Code of Conduct</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-5">\n        <a href="/success-stories/" title="success-stories">Success Stories</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/success-stories/category/arts/" title="">Arts</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/success-stories/category/business/" title="">Business</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/success-stories/category/education/" title="">Education</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/success-stories/category/engineering/" title="">Engineering</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="/success-stories/category/government/" title="">Government</a></li>\n    \n        <li class="tier-2 element-6" role="treeitem"><a href="/success-stories/category/scientific/" title="">Scientific</a></li>\n    \n        <li class="tier-2 element-7" role="treeitem"><a href="/success-stories/category/software-development/" title="">Software Development</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-6">\n        <a href="/blogs/" title="News from around the Python world">News</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/blogs/" title="Python Insider Blog Posts">Python News</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="http://planetpython.org/" title="Planet Python">Community News</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="http://pyfound.blogspot.com/" title="PSF Blog">PSF News</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="http://pycon.blogspot.com/" title="PyCon Blog">PyCon News</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-7">\n        <a href="/events/" >Events</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="/events/python-events" title="">Python Events</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="/events/python-user-group/" title="">User Group Events</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="/events/python-events/past/" title="">Python Events Archive</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/events/python-user-group/past/" title="">User Group Events Archive</a></li>\n    \n        <li class="tier-2 element-5" role="treeitem"><a href="https://wiki.python.org/moin/PythonEventsCalendar#Submitting_an_Event" title="">Submit an Event</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n    <li class="tier-1 element-8">\n        <a href="/dev/" >Contributing</a>\n        \n            \n\n<ul class="subnav menu">\n    \n        <li class="tier-2 element-1" role="treeitem"><a href="https://devguide.python.org/" title="">Developer&#39;s Guide</a></li>\n    \n        <li class="tier-2 element-2" role="treeitem"><a href="https://bugs.python.org/" title="">Issue Tracker</a></li>\n    \n        <li class="tier-2 element-3" role="treeitem"><a href="https://mail.python.org/mailman/listinfo/python-dev" title="">python-dev list</a></li>\n    \n        <li class="tier-2 element-4" role="treeitem"><a href="/dev/core-mentorship/" title="">Core Mentorship</a></li>\n    \n</ul>\n\n        \n    </li>\n    \n</ul>\n\n\n                    <a id="back-to-top-2" class="jump-link" href="#python-network"><span aria-hidden="true" class="icon-arrow-up"><span>&#9650;</span></span> Back to Top</a>\n                    \n\n                </div><!-- end .container -->\n            </div> <!-- end .main-footer-links -->\n\n            <div class="site-base">\n                <div class="container">\n                    \n                    <ul class="footer-links navigation menu do-not-print" role="tree">\n                        <li class="tier-1 element-1"><a href="/about/help/">Help &amp; <span class="say-no-more">General</span> Contact</a></li>\n                        <li class="tier-1 element-2"><a href="/community/diversity/">Diversity <span class="say-no-more">Initiatives</span></a></li>\n                        <li class="tier-1 element-3"><a href="https://github.com/python/pythondotorg/issues">Submit Website Bug</a></li>\n                        <li class="tier-1 element-4">\n                            <a href="https://status.python.org/">Status <span class="python-status-indicator-default" id="python-status-indicator"></span></a>\n                        </li>\n                    </ul>\n\n                    <div class="copyright">\n                        <p><small>\n                            <span class="pre">Copyright &copy;2001-2019.</span>\n                            &nbsp;<span class="pre"><a href="/psf-landing/">Python Software Foundation</a></span>\n                            &nbsp;<span class="pre"><a href="/about/legal/">Legal Statements</a></span>\n                            &nbsp;<span class="pre"><a href="/privacy/">Privacy Policy</a></span>\n                            &nbsp;<span class="pre"><a href="/psf/sponsorship/sponsors/">Powered by Rackspace</a></span>\n                        </small></p>\n                    </div>\n\n                </div><!-- end .container -->\n            </div><!-- end .site-base -->\n\n        </footer>\n\n    </div><!-- end #touchnav-wrapper -->\n\n    \n    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>\n    <script>window.jQuery || document.write(\'<script src="/static/js/libs/jquery-1.8.2.min.js"><\\/script>\')</script>\n\n    <script src="/static/js/libs/masonry.pkgd.min.js"></script>\n\n    <script type="text/javascript" src="/static/js/main-min.js" charset="utf-8"></script>\n    \n\n    <!--[if lte IE 7]>\n    <script type="text/javascript" src="/static/js/plugins/IE8-min.js" charset="utf-8"></script>\n    \n    \n    <![endif]-->\n\n    <!--[if lte IE 8]>\n    <script type="text/javascript" src="/static/js/plugins/getComputedStyle-min.js" charset="utf-8"></script>\n    \n    \n    <![endif]-->\n\n    \n\n    \n    \n\n</body>\n</html>\n'
View Code
 1 from urllib.request import urlopen
 2 fp = urlopen("http://www.python.org")
 3 op = open("python.html","wb")
 4 n = 0
 5 while True:
 6     s = fp.read(1024)
 7     if not s:
 8         break
 9     op.write(s)
10     n = n + len(s)
11 fp.close()
12 op.close()
13 print ("retrieved", n, "bytes from", fp.url)
View Code
1) 使用代理
2) 直接保存URL资源
1 from urllib.request import urlretrieve
2 filename, m = urlretrieve("http://www.baidu.com")
3 filename
4 filename, m = urlretrieve("http://www.baidu.com", filename="I:/book/source/html/baidu.html")
5 filename
View Code
 1 from urllib.request import urlretrieve
 2 def reporthook(block_count, block_size, file_size):
 3     if file_size == -1:
 4         print ("retrieved data", "block_count * bolck_size")
 5     else:
 6         print ("retrieved data", "block_count * bolck_size", "/", "file_size")
 7 urlretrieve("file:///I:/book/source/html/python.html", filename="", reporthook=reporthook)
 8     retrieve data 0 / 16578
 9     retrieve data 8192 / 16578
10     retrieve data 16384 / 16578
11     retrieve data 24576 / 16578
12 urlretrieve("http://www.python.org", filename="", reporthook=reporthook)
13 retrieve data 0 / 16578
14 retrieve data 8192 / 16578
15 retrieve data 16384 / 16578
16 retrieve data 24576 / 16578
17 urlretrieve("http://www.google.com", filename="", reporthook=reporthook)
18 retrieve datad 0
19 retrieve datad 8192
View Code
 1 def download(url, filename=""):
 2     def reporthook(block_count, block_size, file_size):
 3         if file_size == -1:
 4             print("Can't determine the file size, now retrieved", block_count*block_size)
 5         else:
 6             percentage = int((block_count*block_size*100.0)/file_size)
 7             if percentage > 100:
 8                 print("100%")
 9             else:
10                 print("%d%%" % (percentage))
11     filehandler, m = urlretrieve(url, filename, reporthook=reporthook)
12     print("Done")
13 return filehandler
View Code

 13.4.2 分析返回资源的相关信息

1 from urllib.request import urlopen
2 r = urlopen("http://baidu.com")
3 print(r.geturl())
4 print(r.url)
5 # 输出:http://baidu.com
6 # http://baidu.com
View Code
1 from urllib.request import urlopen
2 r = urlopen("http://baidu.com")
3 m = r.info()
4 m.get_countent_type()
5 m.get_countent_maintype()
6 m.get_countent_subtype()
7 print(m)
View Code
 1 from urllib.request import urlopen
 2 r = urlopen("http://baidu.com")
 3 m = r.info()
 4 m.get_countent_type()
 5 m.get_countent_maintype()
 6 m.get_countent_subtype()
 7 m
 8 for k,v in r.info().items():
 9     print (k, "=", v)
10 r.headers
View Code

13.4.3 自定义获取资源方式

1)访问需要简单认证的URL资源
 1 def prompt_user_passwd(self, host, realm):
 2     """Override this in a GUI environment!"""
 3     import getpass
 4     try:
 5         user = input("Enter username for %s at %s:" % (realm, host))
 6         passwd = getpass.getpass("Enter password for %s in %s at %s:" % (user, realm, host))
 7         return user, passwd
 8     except KeyboardInterrupt:
 9         print()
10         return None, None
View Code

 

posted @ 2019-01-04 22:22  无声胜有声  阅读(693)  评论(0编辑  收藏  举报