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. OC跟Swift混编

    OC项目中使用Swift 本文版权归作者所有,如需转载请联系孟祥月 CSDN博客:http://blog.csdn.net/mengxiangyue  独立博客:http://mengxiangyue ...

  2. android api 之Scroller

    Scroller是封装了滚动,实现View和ViewGroup的背景画布的滚动. 它有两个构造方法: public Scroller (Context context) 传递一个上下文. public ...

  3. django 定制admin

    https://www.cnblogs.com/liwenzhou/p/9519321.html

  4. yii2.0安装ElasticSearch及使用

    yii2.0安装ElasticSearch安装及使用教程:https://www.yiichina.com/tutorial/1046 Elasticsearch 权威指南(中文版):https:// ...

  5. centos6.5_64bit_tomcat日志合并在一个.log下

    问题   tomcat每次启动时,自动在logs目录下生产以下日志文件,且每天都会生成对应日期的一个文件,造成日志文件众多:   目的        Tomcat以上日志都输出到同一个文件中.   修 ...

  6. UE4的蓝图都能做什么

    创建关卡脚本 蓝图具有和虚幻3中Kismet一样的功能,每个关卡都由自己的蓝图,他可以: 引用和操控actors 控制使用Matinee的过场 管理关卡流,存档点以及其他关卡相关的系统 和关卡中的类蓝 ...

  7. S/4HANA和CRM Fiori应用的搜索分页实现

    在我的博客Paging Implementation in S/4HANA for Customer Management 我介绍了S/4HANA for Customer Management里采用 ...

  8. 如何使用ABSL代码调用Web service

    需求:在C4C UI里创建web service(maintain ticket),然后通过ABSL代码消费. 1. 创建一个新的Communication Arrangement 基于Manage ...

  9. python 笔记1:官网下载及安装python;eclipse中安装配置pydev

    1  下载安装python. 官网:https://www.python.org/downloads/     根据自己的操作系统选择需要的版本下载并安装. 我的电脑操作系统windows xp的,只 ...

  10. ORA-00392: log 4 of thread 2 is being cleared, operation not allowed

     alter database open resetlogs或者 alter database open resetlogs upgrade报错:ORA-00392 在rman restore 还原数 ...