Python: Access and print a URL's content to the console
Python Basic: Exercise-101 with Solution
Write a Python program to access and print a URL's content to the console.
Sample Solution:-
import urllib.request
result = urllib.request.urlopen("http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=b037")
# retrieves the entire contents.
contents = result.read()
print(contents)
link = 'http://www.somesite.com/details.pl?urn=2344'
f = urllib.urlopen(link)
myfile = f.readline()
print myfile
================= RESTART: F:/Python_APSC/py-ex-basic-101.py =================
b'\n\n\n\n\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n<title>\xe5\x8f\xb0\xe4\xb8\xad\xe5\xa5\xb3\xe4\xb8\xad\xe7\xa8\x8b\xe5\xbc\x8f\xe8\xa7\xa3\xe9\xa1\x8c\xe7\xb3\xbb\xe7\xb5\xb1 (Green Judge, An Online Judge System for TCGS) -- \xe9\xa1\x8c\xe7\x9b\xae\xe5\x85\xa7\xe5\xae\xb9</title>\n\n\n\n\n\n<link rel="stylesheet" type="text/css" media="screen" href="superfish.css" />\n<link rel="stylesheet" type="text/css" media="screen" href="superfish-vertical.css" />\n<link rel="stylesheet" type="text/css" media="screen" href="superfish-navbar.css" />\n<link rel="stylesheet" type="text/css" media="screen" href="style.css" /> \n\n<script language="javascript" src="./jscripts/js_utils.js"></script>\n<script type="text/javascript" src="./jscripts/jquery-1.2.6.min.js"></script>\n<script type="text/javascript" src="./jscripts/hoverIntent.js"></script> \n<script type="text/javascript" src="./jscripts/superfish.js"></script> \n<script type="text/javascript" src="./jscripts/supersubs.js"></script> \n<script type="text/javascript"> \n// initialise plugins\t\n/*\n\tjQuery().ready(function(){\n\t\tjQuery(function(){\n\t\t\tjQuery(\'ul.sf-menu\').superfish();\n\t\t});\t\n\t});\t\t\n*/\n/* For superfish MENU \xef\xbc\x8c IE 6 \xe6\x9c\x83\xe7\x95\xb6\xe6\x8e\x89\xe3\x80\x82 */\n jQuery(document).ready(function(){ \n jQuery("ul.sf-menu").supersubs({ \n minWidth: 2, // minimum width of sub-menus in em units \n maxWidth: 27, // maximum width of sub-menus in em units \n extraWidth: 0 // extra width can ensure lines don\'t sometimes turn over \n // due to slight rounding differences and font-family \n }).superfish(); // call supersubs first, then superfish, so that subs are \n // not display:none when measuring. Call before initialising \n // containing tabs for same reason. \n }); \n</script>\n<!--[if lt IE 7]><script type="text/javascript" src="./jscripts/IE_pngfix.js"></script><![endif]-->\n\n</head>\n\n\n\n<body>\n\n\n\n\n\n\n<div id="banner">\n <div class="FirstPageTitle"><img src="images/tcgs.png" title="\xe5\x8f\xb0\xe4\xb8\xad\xe5\xa5\xb3\xe4\xb8\xad\xe7\xa8\x8b\xe5\xbc\x8f\xe8\xa7\xa3\xe9\xa1\x8c\xe7\xb3\xbb\xe7\xb5\xb1" alt="\xe5\x8f\xb0\xe4\xb8\xad\xe5\xa5\xb3\xe4\xb8\xad\xe7\xa8\x8b\xe5\xbc\x8f\xe8\xa7\xa3\xe9\xa1\x8c\xe7\xb3\xbb\xe7\xb5\xb1"><br />\n Green Judge, An Online Judge System for TCGS</div>\n</div>\n\n\n<!-- SuperFish MENU For FireFox BEGIN **************************************************** -->\n\n<div>\n<ul class="sf-menu">\n\n\n\n <li> | <a class="top_menu" href="./Login">Login</a></li>\n <li> | <a class="top_menu" href="./Register">Register</a></li>\n\n\n\n <li> | <a class="top_menu" href="#">Language</a>\n <ul>\n\t<li class="current"><a href="./ChangeLocale.do?locale=zh_TW">Chinese(TW)</a></li>\n\t<li><a href="./ChangeLocale.do?locale=zh_CN">Chinese(CN)</a></li>\n\t<li><a href="./ChangeLocale.do?locale=en_US">English</a></li>\n </ul>\n </li>\n</ul>\n</div>\n<!-- SuperFish MENU For FireFox END **************************************************** -->\n\n\n\n\n<!-- MAIN MENU -->\n<div class="mainmenu"> <span style="float: none; display: inline">\n <a href="./">HOME</a> | \n <a href="./Problems">Problems</a> | \n <a href="./RealtimeStatus">Status</a> | \n <a href="./RankList">Rank</a> | \n <a href="./Forum">Forum</a> | \n <a href="./ShowContests">Contest</a>\n</span> </div>\n\n<br>\n<div class="content_individual">\n <div style="height:50px;">\n <div style=" width:20%; float:left; font-size:smaller; text-align:left;"><a href="./Problems?tab=tab01">\xe5\x9b\x9e\xe5\x88\x86\xe9\xa1\x9e\xe9\xa1\x8c\xe5\xba\xab</a></div>\n <div style="float:left; width:59%; margin:auto; text-align:center">\n \n <span class="ShowProblem">\n \n \n \n b037: \xe5\xb0\x8f\xe6\x95\xb8\xe7\x9a\x84\xe5\xaf\x86\xe7\xa2\xbc</span></div>\n\t\n <div style=" width:20%; float:right; font-size:smaller; text-align:right; vertical-align:bottom; vertical-align:bottom;">\xe9\x97\x9c\xe9\x8d\xb5\xe5\xad\x97\xef\xbc\x9a\n\t\n\t<a href="./SearchProblem?problemkeyword=%E5%9B%9E%E6%BA%AF%E6%B3%95-%E5%88%97%E8%88%89">\xe5\x9b\x9e\xe6\xba\xaf\xe6\xb3\x95-\xe5\x88\x97\xe8\x88\x89</a>\n\t</div>\n </div>\n <br>\n <div style=" width: 80%; margin:auto; color:#666666; font-size:12px; text-align: right;">\n\xe6\xb8\xac\xe8\xb3\x87\xe9\xbb\x9e : 5 |\nTime Limit : 1000 ms |\nMemory Limit : 32000 KB<br />\n<a href="./RealtimeStatus?problemid=b037&status=AC">Accepted :\n\t267 Times / 226 Users</a> |\n<a href="./RealtimeStatus?problemid=b037">Submit :\n\t421 Times / 237 Users</a> |\nAccepted rate :\n \n \n \n 95%\n \n \n<br />\n\xe9\xa1\x8c\xe7\x9b\xae\xe5\x8a\xa0\xe5\x85\xa5\xe6\x99\x82\xe9\x96\x93 : 2011-11-12 23:52<br />\n </div>\n <p align="left"><strong>\n Content\xef\xbc\x9a</strong> \n\t<a id="translateLink">\xe7\xae\x80\xe4\xbd\x93\xe4\xb8\xad\xe6\x96\x87</a><br>\n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ab\xe4\xbb\x96\xe6\x89\xbe\xe5\x87\xba\xe8\xa6\x81\xe8\xa9\xa6\xe5\xa4\x9a\xe5\xb0\x91\xe5\x80\x8b\xe5\xaf\x86\xe7\xa2\xbc\xe5\x97\x8e\xef\xbc\x9f</div>\n <p align="left"><strong>\n Input\xef\xbc\x9a</strong><br>\n </p>\n <div class="problembox">\xe8\xbc\xb8\xe5\x85\xa5\xe5\x85\xa9\xe5\x80\x8b\xe6\xad\xa3\xe6\x95\xb4\xe6\x95\xb8 N, M (2<=N<=10, 1<=M<=9)\xef\xbc\x8cN \xe4\xbb\xa3\xe8\xa1\xa8\xe5\xaf\x86\xe7\xa2\xbc\xe7\xb8\xbd\xe5\x85\xb1\xe6\x9c\x89\xe5\xb9\xbe\xe5\x80\x8b\xe6\x95\xb8\xe5\xad\x97\xef\xbc\x8cM \xe4\xbb\xa3\xe8\xa1\xa8\xe9\x80\x99\xe4\xba\x9b\xe6\x95\xb8\xe5\xad\x97\xe6\x9c\x80\xe5\xa4\xa7\xe4\xb8\x8d\xe6\x9c\x83\xe8\xb6\x85\xe9\x81\x8e\xe5\xa4\x9a\xe5\xb0\x91\xef\xbc\x8c\xe5\x8d\xb3\xe6\xaf\x8f\xe5\x80\x8b\xe6\x95\xb8\xe5\xad\x97\xe9\x83\xbd\xe6\x98\xaf\xe4\xbb\x8b\xe6\x96\xbc 0~M \xe4\xb9\x8b\xe9\x96\x93\xe3\x80\x82</div>\n \n <p align="left"><strong>\n Output\xef\xbc\x9a</strong><br>\n </p>\n <div class="problembox">\xe8\xab\x8b\xe8\xbc\xb8\xe5\x87\xba\xe5\xb0\x8f\xe6\x95\xb8\xe8\xa6\x81\xe8\xa9\xa6\xe7\x9a\x84\xe6\x89\x80\xe6\x9c\x89\xe5\xaf\x86\xe7\xa2\xbc\xef\xbc\x8c\xe5\xbe\x9e\xe5\xb0\x8f\xe5\x88\xb0\xe5\xa4\xa7\xe8\xbc\xb8\xe5\x87\xba\xef\xbc\x8c\xe6\xaf\x8f\xe8\xa1\x8c\xe8\xbc\xb8\xe5\x87\xba\xe4\xb8\x80\xe5\x80\x8b\xe3\x80\x82</div>\n \n <p align="left" STYLE="font-family:\'Courier New\', Courier, monospace"><strong>\n Sample Input\xef\xbc\x9a</strong><span onmouseover="showHints(event, \'sampleinputhint\');" onmouseout="hideHints(\'sampleinputhint\');"><img src="images/help.png" alt="help"/></span><div id="sampleinputhint" class="hint">\xe8\x8b\xa5\xe9\xa1\x8c\xe7\x9b\xae\xe6\xb2\x92\xe6\x9c\x89\xe7\x89\xb9\xe5\x88\xa5\xe8\xaa\xaa\xe6\x98\x8e\xef\xbc\x8c\xe5\x89\x87\xe6\x87\x89\xe8\xa9\xb2\xe4\xbb\xa5\xe5\xa4\x9a\xe6\xb8\xac\xe8\xb3\x87\xe7\x9a\x84\xe6\x96\xb9\xe5\xbc\x8f\xe8\xae\x80\xe5\x8f\x96\xef\xbc\x8c\xe8\x8b\xa5\xe4\xb8\x8d\xe7\x9f\xa5\xe5\xa6\x82\xe4\xbd\x95\xe8\xae\x80\xe5\x8f\x96\xe8\xab\x8b\xe5\x8f\x83\xe8\x80\x83 a001 \xe7\x9a\x84\xe7\xaf\x84\xe4\xbe\x8b\xe7\xa8\x8b\xe5\xbc\x8f\xe3\x80\x82</div>\n <div class="problembox">\n <pre>2 1</pre>\n </div>\n </p>\n <p align="left" STYLE="font-family:\'Courier New\', Courier, monospace"><strong>\n Sample Output\n \xef\xbc\x9a</strong></p>\n <div class="problembox">\n <pre>00\r\n01\r\n10\r\n11</pre>\n </div>\n <p align="left"><strong>\n Hint\n \xef\xbc\x9a</strong><br>\n </p>\n <div class="problembox">\n \n <p></p>\n </div>\n <p align="left"><strong>\n Author\n \xef\xbc\x9a</strong><br>\n </p>\n <div class="problembox"> \xe5\x9b\x9e\xe6\xba\xaf\xe6\xb3\x95-\xe5\x88\x97\xe8\x88\x89\n (\xe7\xae\xa1\xe7\x90\x86\xe5\x93\xa1\xef\xbc\x9asagit)\n </div>\n <br>\n <div align="center"><a class="buttonlink" href="./SolutionForm?problemid=b037">\xe3\x80\x80\n Solve it!\n\xe3\x80\x80 </a><a class="buttonlink" href="./RealtimeStatus?problemid=b037">\n Status\n </a> <a class="buttonlink" href="./Forum?problemid=b037"> Forum</a>\n </div>\n <br>\n</div>\n\n\n\n\n\n\n\n<br />\n\n<div class="Footer">\n\nPowered by <a href="http://zerojudge.tw/">ZeroJudge</a> R1.2 Built0321\n</div>\n\n<script type="text/javascript" src="./jscripts/tw_cn.js"></script>\n<script type="text/javascript">\nvar defaultEncoding = 1;\nvar translateDelay = 0;\nvar cookieDomain = "./";\nvar msgToTraditionalChinese = "\xe6\xad\xa3\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87";\nvar msgToSimplifiedChinese = "\xe7\xae\x80\xe4\xbd\x93\xe4\xb8\xad\xe6\x96\x87";\nvar translateButtonId = "translateLink";\ntranslateInitilization();\n</Script>\n</body>\n</html>\n'
>>>
沒有留言:
張貼留言