內(nèi)容來源于網(wǎng)絡究孕,本人只是在此稍作整理,如有涉及版權(quán)問題,歸小甲魚官方所有牺堰。
練習題(來自小甲魚官方論壇)
0.請寫下這一節(jié)課你學習到的內(nèi)容:格式不限士修,回憶并復述是加強記憶的好方式章贞!
- URL由三部分組成
1??協(xié)議(不可缺少)
2??存放資源的服務器的域名系統(tǒng)(DNS)主機名或IP地址(有時候需要端口號)(不可缺少)
3??主機資源的具體地址备埃,如目錄或文件名等(可以省略) -
urllib模塊
Python2中urllib還分urllib和urllib2尼桶,到了Python3就只有urllib模塊节槐,這其實也不是一個模塊搀庶,它是一個包(package)。
urllib模塊.png
總共有4個模塊铜异,第一個模塊是最重要的模塊哥倔,它包含了對服務器請求的發(fā)出、跳轉(zhuǎn)揍庄、代理和安全等各個方面咆蒿。
>>> import ssl
>>> import urllib.request
>>> context = ssl._create_unverified_context()
>>> response = urllib.request.urlopen("http://www.fishc.com", context=context)
>>> html = response.read()
>>> print(html)
b'<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <meta name="keywords" content="\xe9\xb1\xbcC\xe5\xb7\xa5\xe4\xbd\x9c\xe5\xae\xa4|\xe5\x85\x8d\xe8\xb4\xb9\xe7\xbc\x96\xe7\xa8\x8b\xe8\xa7\x86\xe9\xa2\x91\xe6\x95\x99\xe5\xad\xa6|Python\xe6\x95\x99\xe5\xad\xa6|Web\xe5\xbc\x80\xe5\x8f\x91\xe6\x95\x99\xe5\xad\xa6|\xe5\x85\xa8\xe6\xa0\x88\xe5\xbc\x80\xe5\x8f\x91\xe6\x95\x99\xe5\xad\xa6|C\xe8\xaf\xad\xe8\xa8\x80\xe6\x95\x99\xe5\xad\xa6|\xe6\xb1\x87\xe7\xbc\x96\xe6\x95\x99\xe5\xad\xa6|Win32\xe5\xbc\x80\xe5\x8f\x91|\xe5\x8a\xa0\xe5\xaf\x86\xe4\xb8\x8e\xe8\xa7\xa3\xe5\xaf\x86|Linux\xe6\x95\x99\xe5\xad\xa6">\n <meta name="description" content="\xe9\xb1\xbcC\xe5\xb7\xa5\xe4\xbd\x9c\xe5\xae\xa4\xe4\xb8\xba\xe5\xa4\xa7\xe5\xae\xb6\xe6\x8f\x90\xe4\xbe\x9b\xe6\x9c\x80\xe6\x9c\x89\xe8\xb6\xa3\xe7\x9a\x84\xe7\xbc\x96\xe7\xa8\x8b\xe8\xa7\x86\xe9\xa2\x91\xe6\x95\x99\xe5\xad\xa6\xe3\x80\x82">\n <meta name="author" content="\xe9\xb1\xbcC\xe5\xb7\xa5\xe4\xbd\x9c\xe5\xae\xa4">\n <title>\xe9\xb1\xbcC\xe5\xb7\xa5\xe4\xbd\x9c\xe5\xae\xa4-\xe5\x85\x8d\xe8\xb4\xb9\xe7\xbc\x96\xe7\xa8\x8b\xe8\xa7\x86\xe9\xa2\x91\xe6\x95\x99\xe5\xad\xa6|Python\xe6\x95\x99\xe5\xad\xa6|Web\xe5\xbc\x80\xe5\x8f\x91\xe6\x95\x99\xe5\xad\xa6|\xe5\x85\xa8\xe6\xa0\x88\xe5\xbc\x80\xe5\x8f\x91\xe6\x95\x99\xe5\xad\xa6|C\xe8\xaf\xad\xe8\xa8\x80\xe6\x95\x99\xe5\xad\xa6|\xe6\xb1\x87\xe7\xbc\x96\xe6\x95\x99\xe5\xad\xa6|Win32\xe5\xbc\x80\xe5\x8f\x91|\xe5\x8a\xa0\xe5\xaf\x86\xe4\xb8\x8e\xe8\xa7\xa3\xe5\xaf\x86|Linux\xe6\x95\x99\xe5\xad\xa6</title>\n <link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">\n <link rel="stylesheet" href="css/styles.css">\n <script src="js/jq.js"></script>\n <script src="https://cdn.bootcss.com/timelinejs/2.36.0/js/storyjs-embed.js"></script>\n <script>\n $(document).ready(function() {\n var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;\n\n createStoryJS({\n type: \'timeline\',\n width: \'auto\',\n height: windowHeight,\n source: \'data.json\',\n start_at_end:true, //OPTIONAL START AT LATEST DATE\n embed_id: \'my-timeline\'\n });\n\n });\n </script>\n <!-- END TimelineJS -->\n</head>\n<body>\n<div id="my-timeline"></div>\n</body>\n</html>'
>>> html = html.decode("utf-8") # 解碼
>>> print(html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="魚C工作室|免費編程視頻教學|Python教學|Web開發(fā)教學|全棧開發(fā)教學|C語言教學|匯編教學|Win32開發(fā)|加密與解密|Linux教學">
<meta name="description" content="魚C工作室為大家提供最有趣的編程視頻教學。">
<meta name="author" content="魚C工作室">
<title>魚C工作室-免費編程視頻教學|Python教學|Web開發(fā)教學|全棧開發(fā)教學|C語言教學|匯編教學|Win32開發(fā)|加密與解密|Linux教學</title>
<link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">
<link rel="stylesheet" href="css/styles.css">
<script src="js/jq.js"></script>
<script src="https://cdn.bootcss.com/timelinejs/2.36.0/js/storyjs-embed.js"></script>
<script>
$(document).ready(function() {
var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
createStoryJS({
type: 'timeline',
width: 'auto',
height: windowHeight,
source: 'data.json',
start_at_end:true, //OPTIONAL START AT LATEST DATE
embed_id: 'my-timeline'
});
});
</script>
<!-- END TimelineJS -->
</head>
<body>
<div id="my-timeline"></div>
</body>
</html>
我們要對拿到的字節(jié)對象進行解碼蚂子,把他變成Unicode編碼沃测,這樣我們才能看懂。
備注:目前魚C已經(jīng)是https食茎,所以無法用書上的內(nèi)容爬取蒂破。修改方式參考如下鏈接:https://blog.csdn.net/xiaopangxia/article/details/49908889
- 編碼