Python学习-19.Python的Http模块
模拟 http 请求是比较常见的一种需求,在 Python 中,使用 http 模块操作。
import http.client # 创建 Http 连接。
http = http.client.HTTPConnection('www.baidu.com')
# 设置请求,第一个参数为请求方法,第二个参数为请求的页面。
http.request('GET','/')
# 获取百度主页的内容,返回的是一个 bytes。
html = http.getresponse().read()
# 关闭 Http 连接。
http.close() # 新建文件,并使用 utf-8 编码,否则写入时会出错。
file = open(r'e:\temp\baidu.html','w',encoding='utf-8')
# 写入解码后的 Html 内容。
file.write(html.decode())
# 关闭文件。
file.close()
值得注意的是,我们需要设置好编码,在Python中,open新建的文件默认是ascii的,而我们获取到的内容是unicode的,因此直接写入的话会出错。在命令行模式下输出同理,因为命令行的编码模式是gbk。
Python学习-19.Python的Http模块的更多相关文章
- 《转》Python学习(19)-python函数(二)-关于lambda
转自http://www.cnblogs.com/BeginMan/p/3178103.html 一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称 ...
- Python学习day17-常用的一些模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习系列(六)(模块)
Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...
- python学习第四十八天json模块与pickle模块差异
在开发过程中,字符串和python数据类型进行转换,下面比较python学习第四十八天json模块与pickle模块差异. json 的优点和缺点 优点 跨语言,体积小 缺点 只能支持 int st ...
- Python学习day09 - Python进阶(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day05 - Python基础(3) 格式化输出和基本运算符
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- python学习笔记-(九)模块
基础知识 1. 定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑----实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块就是test) 包:用 ...
- Python学习之路13☞常用模块
一 time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...
- python学习笔记之九:模块和包
Python的标准安装包括一组模块,称为标准库.这里介绍模块的工作方式,学习如何使用它们. 一. 模块 1.1 用import从外部模块获取函数并为自己的程序所用: >>> from ...
随机推荐
- VScode 安装必备
1.运行程序:
- mybatis3 @SelectProvider
mybatis3中增加了使用注解来配置Mapper的新特性,本篇文章主要介绍其中几个@Provider的使用方式,他们是:@SelectProvider.@UpdateProvider.@Insert ...
- webdriver简介及浏览器的驱动
1.webdriver概述: webdriver(selenium2=selenium1+webdriver)是一种用于web应用程序的自动化测试工具,它提供了一套友好的API,与selenium ...
- BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource
Tomcat报错如下: BeanDefinitionStoreException: IOException parsing XML document from ServletContext resou ...
- 第五章 二叉树(e1)先序遍历
- 第八章 高级搜索树 (b4)B-树: 插入
- GridView,datalist添加序号列
GridView添加序号列:这个是经常需要的一个功能 <asp:TemplateField HeaderText="序号"> <ItemTemplate> ...
- 获取客户端真实IP地址
Java-Web获取客户端真实IP: 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP. 一般分为两种情况: ...
- ios 工具大全,最全框架
https://www.jianshu.com/p/e280f3348156 [链接]文件分享 - 网盘分享https://share.weiyun.com/5A1aura
- Ubuntu-18.04Python2与Python3自由切换
一.配置ssh链接 安装openssh-server devops@devops-virtual-machine:~$ sudo apt-get install openssh-server 二.安装 ...