以下是三种方法

①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. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo-client'.

    cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ...

  2. EPEL 安装源

    EPEL 安装源 EPEL 是 Extra Packages for Enterprise Linux 的缩写(EPEL),是用于 Fedora-based Red Hat Enterprise Li ...

  3. DOM解析和优化

    DOM解析 1. css不会阻塞DOM解析(DOM Tree),但会阻塞DOM渲染(css Tree + DOM Tree -> render Tree )2. JS阻塞DOM解析,但浏览器会预 ...

  4. 关于ofbiz13.07和ofbiz14.12部署的问题解决方法

    必备软件 Jdk7 文件名:java_ee_sdk-6u4-jdk7-windows.exe eclipse MySQL Installer 5.6 for Windows MySql Connect ...

  5. ASP.NET三层架构的优点和缺点

    原文发布时间为:2009-10-24 -- 来源于本人的百度文章 [由搬家工具导入] 小项目,以后变动不大的不用三层架构。 ASP.NET三层结构说明 完善的三层结构的要求是:修改表现层而不用修改逻辑 ...

  6. 恶补一下DP+背包专题(刷刷水题)L2

    开心的金明 题目大意 就是求一定背包容量的最大值 思路 想必大家都知道,一看到这种题目,就会想起01背包 虽然特别简单但是还是讲一下吧 状态设置 由于这题差不多是一个01背包的版子题,那么我们就只需要 ...

  7. Windows开发

    1. 介绍 这里简单介绍了Windows应用程序开发的基础知识 2. 基础 Windows下的应用程序有控制台程序和Win32窗口程序,这里讲的是Win32窗口程序 Windows提供了相关静态库(L ...

  8. python安装在windows server2008,故障排除

    python安装在windows server2008,故障排除   我也在虚拟机上的win2008安装python2.7.9多次回滚了.搜了一通 Windows Server 2003/2008无法 ...

  9. Product of Array Except Self - LeetCode

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  10. Linux 安装 Java 运行环境

    方式一 # 简单粗暴流# 我使用的是 ubuntu server 18.04 LTS 版本的系统 不同的系统的命令可能存在差异# 在命令行使用java 回车 发现没有Java的话 下面会提示 安装的方 ...