以下是三种方法

①First Method

最简单的方法

②添加data,http header

使用Request对象

③CookieJar

import urllib.request
from http import cookiejar
url ='http://www.baidu.com' print("First Method") response1 = urllib.request.urlopen(url)
#返回状态码
print(response1.getcode())
print(len(response1.read())) print("Second Method")
request = urllib.request.Request(url)
request.add_header("uese-agent","Mazilla/5.0")
response2 = urllib.request.urlopen(url)
#返回状态码
print(response2.getcode())
print(len(response2.read())) print("Third Method")
#声明一个CookieJar对象实例来保存cookie
cj = cookiejar.CookieJar()
#利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler
handler = urllib.request.HTTPCookieProcessor(cj)
#通过CookieHandler创建opener
opener = urllib.request.build_opener(handler)
#此处的open方法同urllib.request的urlopen方法,也可以传入request
response3 = opener.open(url)
#返回状态码
print(response3.getcode())
print(response3.read())

【Python】python3中urllib爬虫开发的更多相关文章

  1. Python2和Python3中urllib库中urlencode的使用注意事项

    前言 在Python中,我们通常使用urllib中的urlencode方法将字典编码,用于提交数据给url等操作,但是在Python2和Python3中urllib模块中所提供的urlencode的包 ...

  2. 常见的爬虫分析库(1)-Python3中Urllib库基本使用

    原文来自:https://www.cnblogs.com/0bug/p/8893677.html 什么是Urllib? Python内置的HTTP请求库 urllib.request          ...

  3. Python3中Urllib库基本使用

    什么是Urllib? Python内置的HTTP请求库 urllib.request          请求模块 urllib.error              异常处理模块 urllib.par ...

  4. python3中urllib库的request模块详解

    刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得厚着脸皮转过来啊! 原帖地址:https://www.2cto.com/kf/201801/714859.html 什么是 Urlli ...

  5. Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)

    urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的数据进行保存哦,下面整理了一些关于urllib使用中的一些 ...

  6. Python3中urllib详细使用方法(header,代理,超时,认证,异常处理) 转

    urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的数据进行保存哦,下面整理了一些关于urllib使用中的一些 ...

  7. python3中urllib的基本使用

    urllib 在python3中,urllib和urllib2进行了合并,现在只有一个urllib模块,urllib和urllib2的中的内容整合进了urllib.request,urlparse整合 ...

  8. Python3中urllib使用介绍

    Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用import urllib2——-对应的,在Python3.x中会使用import url ...

  9. Python3中urllib使用与源代码

    Py2.x: Urllib库 Urllin2库 Py3.x: Urllib库 变化: 在Pytho2.x中使用import urllib2---对应的,在Python3.x中会使用import url ...

随机推荐

  1. hdu 3721 树的直径

    思路:枚举+树的直径 #include<iostream> #include<cstring> #include<cstdio> #include<algor ...

  2. 浅谈C++三层架构

    浅谈C++三层架构 三层架构并不是MVC,MVC是一个很早就有的经典的程序设计模式,M-V-C分为三层,M(Model)-V(View)-C(Control). web开发中的三层架构是指:数据访问层 ...

  3. MSP430之自动增益程控放大main备份

    占位符 #include <msp430.h> #include "sys.h" #include "ps2.h" #include "1 ...

  4. Python之文件操作:文件、目录的操作

    一.创建 1.创建文件 open(path,'w') 2.创建目录 (1)os.mkdir(pt[, mode=0777]) 新建一个目录pt,参数mode表示生成的目录的权限,默认是超级权限,也就是 ...

  5. POJ 2168 Popular cows [Tarjan 缩点]

                                                                                                         ...

  6. input file 修改按钮名称

    解决方法: 1)页面上放个隐藏的<input type=“file” /> 2)然后加上一个文本input(type="text")和一个按钮input(type=&q ...

  7. 生成静态页面方法 .NET

    原文发布时间为:2009-09-30 -- 来源于本人的百度文章 [由搬家工具导入] 采用模板法:【例子中的两个页面以及生成的页面均在同一个目录,自己可以去改】 模板Template.htm: < ...

  8. 全面解析Vue.nextTick实现原理

    vue中有一个较为特殊的API,nextTick.根据官方文档的解释,它可以在DOM更新完毕之后执行一个回调,用法如下: // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 V ...

  9. [LeetCode] Permutations II 排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  10. java 去html标签,去除字符串中的空格,回车,换行符,制表符

    public static String getonerow(String allLine,String myfind)     {                           Pattern ...