分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

01-url-open_code

发布时间:2023-09-06 02:35责任编辑:顾先生关键词:url
import urllib.requestdef 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()

01-url-open_code

原文地址:https://www.cnblogs.com/hankleo/p/10591173.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved