地址栏错误
import urllib.request
import urllib.error
import urllib.parse
req = urllib.request.Request('http://www.mycodr.cn')
try:
r = urllib.request.urlopen(req)
print(r.read())
except urllib.error.URLError as e:
print(e)
print(e.reason)
---
<urlopen error [Errno 11001] getaddrinfo failed>
[Errno 11001] getaddrinfo failed
编码错误
try:
r = urllib.request.urlopen('https://www.douban.com/aba.html')
print(r.read())
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode())
---
404
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>404 Not Found</title>
<style type="text/css">
body{font-family:Arial,Helvetica,sans-serif;font-size:14px;}
h1{font-size:25px;margin:25px 0 10px 0;}
</style>
</head>
<body>
<div>
<div style="margin:20px auto;width:960px">
<div style="font-size:25px;color:#1b9336;border-bottom:5px solid #eef9eb">
<span style="font-size:20px;font-weight:bold">豆瓣</span> d<span style="color:#0092c8">o</span><span style="color:#ffad68">u</span><span>b</span><span style="color:#0092c8">a</span><span style="color:#ffad68">n</span>
</div>
<h1>Not Found</h1>
<div><p>你要的东西不在这, 到别处看看吧。</p></div>
</div>
</div>
</body>
</html>
网友评论