前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1.01-url-open_code

1.01-url-open_code

作者头像
hankleo
发布2020-09-17 10:32:38
2130
发布2020-09-17 10:32:38
举报
文章被收录于专栏:Hank’s BlogHank’s Blog
代码语言:javascript
复制
import urllib.request

def load_data():
    url = "http://www.baidu.com/"
    #get的请求
    #http请求
    #response:http相应的对象
    response = urllib.request.urlopen(url)
    print(response)
    #读取内容 bytes类型
    data = response.read()
    print(data)
    #将文件获取的内容转换成字符串
    str_data = data.decode("utf-8")
    print(str_data)
    #将数据写入文件
    with open("baidu.html","w",encoding="utf-8")as f:
        f.write(data)
    #将字符串类型转换成bytes
    str_name = "baidu"
    bytes_name =str_name.encode("utf-8")
    print(bytes_name)

    #python爬取的类型:str bytes
    #如果爬取回来的是bytes类型:但是你写入的时候需要字符串 decode("utf-8")
    #如果爬取过来的是str类型:但你要写入的是bytes类型 encode(""utf-8")
load_data()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档