python3对urllib和urllib2进行了重构,拆分成了urllib.request,urllib.response, urllib.parse, urllib.error等几个子模块,这样的架构从逻辑和结构上说更加合理。urllib库无需安装,python3自带。python 3.x中将urllib库和urilib2库合并成了urllib库。 其中

urllib2.urlopen() 变成了 urllib.request.urlopen()
urllib2.Request() 变成了 urllib.request.Request()
python2中的 cookielib 改为 http.cookiejar.
import http.cookiejar 代替  import cookielib
urljoin 现在对应的函数是 urllib.parse.urljoin

import urllib.request
import http.cookiejar url ="http://www.baidu.com" print ('第一种方法')
response1=urllib.request.urlopen(url)
print (response1.getcode())
print (len(response1.read())) print ('第二种方法')
request=urllib.request.Request(url)
request.add_header("user-agent","Mozilla/5.0")#将爬虫伪装成浏览器
response2=urllib.request.urlopen(request)
print (response2.getcode())#打印状态码
print (len(response2.read()))#打印内容长度 print ('第三种方法')
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
response3=urllib.request.urlopen(url)
print (response1.getcode())
print (cj) #输出cookie
print (response1.read())

参考链接:https://blog.csdn.net/weixin_43550140/article/details/84563205

python3对urllib和urllib2进行了重构的更多相关文章

  1. python3的urllib以及urllib2的报错问题

    1. urllib.urlencode(params) 换成 urllib.parse.urlencode(params) 2. 在python3.3后urllib2已经不能再用,只能用urllib. ...

  2. ┱Python中关于urllib和urllib2的问题

    python3对urllib和urllib2进行了重构主要拆分成了:1.urllib.request 1.urllib.request.Request(url, data=None, headers= ...

  3. 详解:Python2中的urllib、urllib2与Python3中的urllib以及第三方模块requests

    在python2中,urllib和urllib2都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: 1.urllib2可以接受一个Request类的实例来设置URL请求的hea ...

  4. Python2中的urllib、urllib2和 Python3中的urllib、requests

    目录 Python2.x中 urllib和urllib2 常用方法和类 Python3.x中 urllib requests Python2.x中 urllib和urllib2 urllib 和 ur ...

  5. 深入理解urllib、urllib2及requests

    urllib and urllib2 区别 –博主提示:下面的是python2中的用法,python3需要做出相应修改. urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功 ...

  6. python爬虫入门(一)urllib和urllib2

    爬虫简介  什么是爬虫? 爬虫:就是抓取网页数据的程序. HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的 ...

  7. urllib、urllib2、urllib3区别和使用

    python3中把urllib和urllib合并为一个库了,urllib对应urllib.request 1.) python 中最早内置拥有的网络请求模块就是 urllib,我们可以看一下 urll ...

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

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

  9. python3: 爬虫---- urllib, beautifulsoup

    最近晚上学习爬虫,首先从基本的开始: python3 将urllib,urllib2集成到urllib中了, urllib可以对指定的网页进行请求下载,  beautifulsoup 可以从杂乱的ht ...

随机推荐

  1. codevs 3304 水果姐逛水果街Ⅰ

    这道题可以用ST表过: 题目链接 记录4个数组:maxval[][], minval[][], ans[][], rans[][] maxval[i][j]表示从i号元素开始,长度为(1<< ...

  2. xLua 学习

    xLua https://github.com/Tencent/xLua 文档 https://tencent.github.io/xLua/public/v1/guide/index.html FA ...

  3. Luogu P5022 旅行

    开始写复赛题了 先放张图纪念我惨烈的卡常之路 不说了,简直悲伤 题目链接 思路么..不想写了 Code //不要在意四十行超级加速,卡常用的 #include<bits/stdc++.h> ...

  4. Python Dataframe 分组排序和 Modin

    Python Dataframe 分组排序和 Modin 1.按照其中一列进行排序 在dataframe中,按照其中的一列排序:比如q值倒排 (1)rank方法 data['new_rank'] = ...

  5. HMM AND CRF

    Structured Learning 4: Sequence Labeling:https://www.youtube.com/watch?v=o9FPSqobMys HMM crf 李宏毅老师讲的 ...

  6. 【转】np.random.random()函数 参数用法以及numpy.random系列函数大全

    转自:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.random((1000, 20) ...

  7. pixijs shader 扫光加强版

    pixijs shader 扫光加强版 const app = new PIXI.Application({ transparent: true }); document.body.appendChi ...

  8. 关于 ReadOnlySpan<T>

    using System; using System.Linq; namespace BenchmarkAndSpanExample { public class NameParser { publi ...

  9. Kubernetes Ingress 部署

    Kubernetes Ingress 部署 Pod与Ingress的关系• 通过service相关联• 通过Ingress Controller实现Pod的负载均衡- 支持TCP/UDP 4层和HTT ...

  10. Git以及GitHub的一些基本使用

    1:注册GitHub账号: https://github.com/ 2:Git bash工具下载地址 https://gitforwindows.org/ 3:怎么在GitHub 新增 SSH Key ...