python3如何使用urllib2的类库获取网页的源码?

python3之后已经改版了,我们只需要换一个使用的方法就可以了如下所示:

import urllib.request
# 向指定URL发送请求并且返回服务器响应的类文件对象
response=urllib.request.urlopen("http://www.baidu.com")
# 服务器返回的类文件支持python的对象文件操作
# read()读取URL里的所有文件
html=response.read()

print(html)

请叫我程序员雷锋,不用谢!

发布了152 篇原创文章 · 获赞 141 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_44739706/article/details/104987449