模拟 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模块的更多相关文章

  1. 《转》Python学习(19)-python函数(二)-关于lambda

    转自http://www.cnblogs.com/BeginMan/p/3178103.html 一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称 ...

  2. Python学习day17-常用的一些模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. Python学习系列(六)(模块)

    Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...

  4. python学习第四十八天json模块与pickle模块差异

    在开发过程中,字符串和python数据类型进行转换,下面比较python学习第四十八天json模块与pickle模块差异. json 的优点和缺点 优点  跨语言,体积小 缺点 只能支持 int st ...

  5. Python学习day09 - Python进阶(3)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  6. Python学习day05 - Python基础(3) 格式化输出和基本运算符

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  7. python学习笔记-(九)模块

    基础知识 1. 定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑----实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块就是test) 包:用 ...

  8. Python学习之路13☞常用模块

    一 time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...

  9. python学习笔记之九:模块和包

    Python的标准安装包括一组模块,称为标准库.这里介绍模块的工作方式,学习如何使用它们. 一. 模块 1.1 用import从外部模块获取函数并为自己的程序所用: >>> from ...

随机推荐

  1. General error 2006 MySQL server has gone away

    写入配置文件的办法: max_allowed_packet = 16M //但是这种有时候不支持,1024*1024*16这种有的也不支持 max_allowed_packet = 16777216 ...

  2. airway之workflow

    1)airway简介 在该workflow中,所用的数据集来自RNA-seq,气道平滑肌细胞(airway  smooth muscle cells )用氟美松(糖皮质激素,抗炎药)处理.例如,哮喘患 ...

  3. html中的innerHTML

    首先看个例子 <html> <head> <script type="text/javascript"> function getInnerHT ...

  4. 关于插入date型数据

    create table student (name varchar2(10) not null primary key , enrolldate date not null);//创建student ...

  5. client / server端用户的登录

    # 客户端 import socket import hashlib import json import os import struct sk = socket.socket() # 实例化 sk ...

  6. ECMAScript3的原型

    function Super(){ // 父类 } function Sub(){ // 子类 } Sub.prototype = new Super(); Sub.prototype.constru ...

  7. 4-js 函数

    总是有些奇奇怪怪的问题: <div> <p class="productStatus"> <span>成交量 <em>${goods ...

  8. ajax访问当前页面后的 [WebMethod]描述的方法

    脚本: function show() { $.ajax({ type: "post", async: false, contentType: "application/ ...

  9. JTemplate学习(二)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DT ...

  10. sqlserver 日期格式化

    Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVE ...