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)

urllib使用一的更多相关文章

  1. python urllib

    在伴随学习爬虫的过程中学习了解的一些基础库和方法总结扩展 1. urllib 在urllib.request module中定义下面的一些方法 urllib.request.urlopen(url,d ...

  2. Python3使用urllib访问网页

    介绍 改教程翻译自python官网的一篇文档. urllib.request是一个用于访问URL(统一资源定位符)的Python模块.它以urlopen函数的形式提供了一个非常简单的接口,可以访问使用 ...

  3. 爬虫初探(1)之urllib.request

    -----------我是小白------------ urllib.request是python3自带的库(python3.x版本特有),我们用它来请求网页,并获取网页源码. # 导入使用库 imp ...

  4. python 3.x urllib学习

    urllib.request import urllib.request as ur url='http://ie.icoa.cn' user_agent = 'Mozilla/4.0 (compat ...

  5. Python爬虫学习(1): urllib的使用

    1.urllib.urlopen 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作 In [1]: import urllibIn [2]: file = urllib.urlo ...

  6. python2 与 python3 urllib的互相对应关系

    urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...

  7. urllib+BeautifulSoup无登录模式爬取豆瓣电影Top250

    对于简单的爬虫任务,尤其对于初学者,urllib+BeautifulSoup足以满足大部分的任务. 1.urllib是Python3自带的库,不需要安装,但是BeautifulSoup却是需要安装的. ...

  8. 初学python之urllib

    urllib.request urlopen()urllib.urlopen(url, data, proxies) :创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远 ...

  9. urllib.urlretrieve的用法

    urllib.urlretrieve(url, local, cbk) urllib.urlretrieve(p,'photo/%s.jpg'%p.split('/')[-4]) url要下载的网站 ...

  10. 关于python3.X 报"import urllib.request ImportError: No module named request"错误,解决办法

    #encoding:UTF-8 import urllib.request url = "http://www.baidu.com" data = urllib.request.u ...

随机推荐

  1. canvas制作雪花效果

    <!DOCTYPE html><html> <head>    <meta http-equiv="Content-type" conte ...

  2. php的yii框架开发总结5

    MVC架构之model类: 我的日报系统用到的数据表:tbl_dailyreport表 其中anthor_id是外键,对应tbl_user数据表的主键id,下面是tbl_user表 class Dai ...

  3. AOP术语分析

    初看这么多术语,一下子都不好接受,慢慢来,很快就会搞懂. 通知.增强处理(Advice) 就是你想要的功能,也就是上说的安全.事物.日子等.你给先定义好,然后再想用的地方用一下.包含Aspect的一段 ...

  4. April 24 2017 Week 17 Monday

    Much effort, much prosperity. 越努力,越幸运. I have ever seen this sentence in many people's signature of ...

  5. 【JavaScript 封装库】BETA 2.0 测试版发布!

    /* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...

  6. IOS 制作动画代码和 设置控件透明度

    方式1: //animateWithDuration用1秒钟的时间,执行代码 [UIView animateWithDuration:1.0 animations:^{ //存放需要执行的动画代码 s ...

  7. 【CCPC-Wannafly Winter Camp Day3 (Div1) G】排列(水题)

    点此看题面 大致题意:已知 \(p\)为\(n\)的一个排列,定义\(A(p)_i=min_{j=1}^ip_j\),若用\(q_i\)表示\(p\)第\(i\)小的前缀的长度(以值为第一关键字,下标 ...

  8. HDU 1532

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题意: 三叶草是这个人的最喜欢的植物,结果下雨淹没了他家里,要排水,一个点到一个点的排水速度已知 ...

  9. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  10. spring boot1.5.6 测试类

    package com.qutaoyao.demo.web; import com.qutaoyao.demo.web.controller.HelloController;import org.ju ...