python3之后版本读取网页的内容

import urllib.request
url = "http://helloworldbook2.com/data/message.txt"
#直接通过url来获取网页数据
print('第一种')
response = urllib.request.urlopen(url)
code = response.getcode()
html = response.read()
mystr = html.decode("utf8")
response.close()
print(mystr)

输出结果如下

第一种
Congratulations! You have just made your computer reach across the internet to get a secret message!
I hope you are enjoying "Hello World! Computer Programming for Kids and Other Beginners".
Have fun!

Warren and Carter
发布了53 篇原创文章 · 获赞 1 · 访问量 1147

猜你喜欢

转载自blog.csdn.net/CHYabc123456hh/article/details/104546737