【python】urlopen学习

【python】urlopen学习,第1张

目录
  • 一、基础知识
  • 二、获取信息

一、基础知识
# 获取页面编码格式
if __name__ == '__main__':
    # urlopen()方法,可以使用read()进行读取,也可以使用geturl()、info()、getcode()
    # geturl():返回url的字符串
    # info():返回meta标记的元信息,包括服务器信息
    # getcode():返回的是http的状态码
    response = request.urlopen("http://fanyi.baidu.com")
    html_read = response.read()
    charset = chardet.detect(html_read)
    print(charset)

    #
    req = request.Request("http://www.baidu.com")
    response = request.urlopen(req)
    html = response.read()
    html = html.decode("utf-8")
    print(html)
二、获取信息
from urllib import request

if __name__ == '__main__':
    req = request.Request("http://fanyi.baidu.com")
    response = request.urlopen(req)
    print("请求url:\t%s" % (response.geturl()))
    print('========================')
    print("【响应体】:\n%s" % (response.info()))
    print('========================')
    print("【响应码】:\t%s" % (response.getcode()))

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/904601.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存