import urllib.request   #python3中将urllib2拆分为了urllib.request、urllib.error、urllib.response等
import http.cookiejar url = "http://www.baidu.com" print("第一种方法")
response1 = urllib.request.urlopen(url)
print(response1.getcode()) #打印response1的状态码看是否请求成功, 200表示请求成功
print(len(response1.read())) #打印返回网页内容长度 print("第二种方法")
request = urllib.request.Request(url)
request.add_header("user-agent", "Mozilla/5.0") #模拟浏览器访问
request2 = urllib.request.urlopen(request)
print(request2.getcode())
print(len(request2.read())) print("第三种方法")
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) #在请求对象中添加cookie
urllib.request.install_opener(opener)
request3 = urllib.request.urlopen(url)
print(request3.getcode())
print(len(request3.read()))

最新文章

  1. Linux下串口编制【转】
  2. struts2-json-plugin插件实现异步通信
  3. python_way,day3 集合、函数、三元运算、lambda、python的内置函数、字符转换、文件处理
  4. Windows电脑上安装Appium
  5. Linux网络编程3——socket
  6. windows创建虚拟界面
  7. Stage3D学习笔记(六):旋转动画效果
  8. Selenium+Python之163邮件发送
  9. Swift迁入第三方库时的版本错误解决
  10. Dubbo负载均衡策略
  11. 菜鸟之路Vue----一
  12. Redis分布式集群搭建
  13. Node Express 初探
  14. react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行
  15. 【HNOI 2017】影魔
  16. c# 线程锁 ,
  17. A1016. Phone Bills
  18. ionic toggle点击返回true/false支持自定义
  19. php初级之数组与 类初级
  20. PHP源码编译安装

热门文章

  1. Topcoder SRM584 DIV 2 500
  2. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
  3. js动画之简单运动二
  4. 【转】MSM搭建(Memcached_Session_Manager)--解决集群session共享
  5. 例子:Background Agent Sample
  6. 原生javascript和jquery实现简单的ajax例子
  7. 《C++primer》v5 第3章 字符串、向量和数组 读书笔记 习题答案
  8. springmvc学习第四天
  9. html5有哪些新特性、移除了那些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分 HTML 和 HTML5?
  10. json_decode时含有中文是解码问题(json_decode返回为null)