《python程序设计基础与应用》书p.186
13.2.1(1)
错误情况:
在这里插入图片描述
SyntaxError: multiple statements found while compiling a single statement
SyntaxError:编译单个语句时发现多个语句

奇怪点如下

代码:

import urllib.request
fp=urllib.request.urlopen(r'http://www.python.org')
print(fp.read(100))
print(fp.read(100).decode())
fp.close()

结果:
在这里插入图片描述
删除decode(),结果同上。

再删除read(100)里的100:

import urllib.request
fp=urllib.request.urlopen(r'http://www.python.org')
print(fp.read())
print(fp.read())
fp.close()

结果:
在这里插入图片描述
发现行数变多了,无格式。

另一种代码:

import urllib.request
response = urllib.request.urlopen('http://python.org/')
result = response.read().decode()
print(result)  

示例结果:

在这里插入图片描述
我的结果:
在这里插入图片描述
未编码结果:
在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐