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

最新文章

  1. ASP.NET 5 (vNext) Linux部署
  2. react7 react 三目运算
  3. 基于cmake编译安装MySQL-5.5
  4. CC3000 SPI接口编程介绍
  5. 将json转换成struts参数
  6. 关于stacking context和CSS z-index的总结
  7. Why Deep Learning Works – Key Insights and Saddle Points
  8. win8 中实现断点续传
  9. flex 调用gp服务
  10. 51NOD1433] 0和5(数论,规律)
  11. Linux命令-date
  12. 使用Fiddler提高前端工作效率 (实例篇)
  13. java学习之线程
  14. 【笔记】nodejs读取JSON,数组转树
  15. [SCOI2009]windy数
  16. TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable—Jason niu
  17. GitFlow原理浅析
  18. [Converge] Gradient Descent - Several solvers
  19. vue.js精讲02
  20. [日常] Go语言圣经--复数,布尔值,字符串习题

热门文章

  1. Paper | Fast image processing with fully-convolutional networks
  2. Spring框架spring-web模块中的RestTemplate类详解
  3. 【转】pywinauto教程
  4. 入门理解mysql-binlog
  5. BIM软件Revit的优点
  6. 项目整合SpringDataRedis
  7. div+css画一个小猪佩奇
  8. 一个比 AutoMapper 更快的模型映射的组件 Mapster
  9. 超详细国外VPS搭建教程
  10. Python platform 模块