urllib.urlopen()方法:

参数:

1.url(要访问的网页链接http:或者是本地文件file:)

2.data(如果有,就会由GET方法变为POST方法,提交的数据格式必须是application/x-www-form-urlencoded格式)

返回值:

返回类文件句柄

常用方法

read(size)--size=-1/None,读取多少字节数据取决于size的值,负数就是读取全部内容,默认省略size然后读取全部

readline()读取一行

readlines()读取所有行,返回列表

close()

getcode()返回http请求应答码

urllib基本使用:

一、打印输出100字节

import urllib

html = urllib.urlopen("http://www.runoob.com/python/python-email.html")
print(html.read(100))

打印结果:

<!Doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

如果不设定read(size)size参数,就会全部读取

二、readline()

import urllib

html = urllib.urlopen("http://www.runoob.com/python/python-email.html")
print(html.readline())

读取一行内容出来

运行结果:

<!Doctype html>

for循环遍历几行出来

import urllib

html = urllib.urlopen("http://www.runoob.com/python/python-email.html")
for i in range(10): print("line %d: %s"%(i+1,html.readline()))

运行结果:

line 1: <!Doctype html>

line 2: <html>

line 3: <head>

line 4: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

line 5: <meta property="qc:admins" content="" />

line 6: <meta name="viewport" content="width=device-width, initial-scale=1.0" />

line 7: <title>Python SMTP发送邮件 | 菜鸟教程</title>

line 8: <link rel='dns-prefetch' href='//s.w.org' />

line 9: <link rel="canonical" href="http://www.runoob.com/python/python-email.html" />

line 10: <meta name="keywords" content="Python SMTP发送邮件">

三、readlines()方法

import urllib

html = urllib.urlopen("http://www.runoob.com/python/python-email.html")
print(html.readlines())

四、getcode()方法

import urllib

html = urllib.urlopen("http://www.runoob.com/python/python-email.html")
print(html.getcode())

返回200 OK状态码

定义打印列表方法,后面会用到

def print_list(lists):
for i in lists:
print(i)

最新文章

  1. grub2添加win引导(未试用)
  2. static的用法
  3. 颜色渐变的JS代码
  4. c++ 调用dll
  5. 循环遍历泛型集合List绑定到table
  6. 【模板】【转载】区间dp
  7. 【读书笔记】iOS-GCD-block-后台运行
  8. 如何使用GitHub?
  9. jQuery:多个AJAX/JSON请求对应单个回调并行加载
  10. JAVA多线程的问题以及处理【转】
  11. android之ListView,详细介绍实现步骤,举例,自定义listview适配器
  12. powerdesigener 12.5注册机
  13. zoj 1134 - Strategic Game
  14. 一天搞定CSS: overflow--14
  15. github管理的建立(SSH Key生成步骤)
  16. 花点时间顺顺Git(下)
  17. max (Largest elements in array)
  18. vuex的module的简单实用方法
  19. Bioconductor应用领域之基因芯片
  20. part1:6-Linux文本编辑器vi

热门文章

  1. CSS基础必备盒模型及清除浮动
  2. document.URL 和 windows.location.href的区别
  3. Miner3D Basic基础版
  4. System.Data.SqlClient.SqlException: 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
  5. 爱上python(几个小例子)
  6. Linux命令之文件重定向2
  7. 封装win7系统、制作win7GHO镜像、制作一个自定义的镜像文件具体步骤、制作Win10镜像gho
  8. MySQL数据库实验五:数据更新
  9. Centos 5.5 编译安装mysql 5.5.9
  10. python 面向对象(一)--类(class)和实例(Instance)