使用urllib2打开网页的三种方法(Python2)
python2才有urllib2模块,python3把urllib和urllib2封装成了urllib模块
使用urllib2打开网页的三种方法
#coding:utf-8
import urllib2
import cookielib url="http://www.baidu.com" print '方法 1' response1=urllib2.urlopen(url)
print response1.getcode()#验证打开网页是否成功,成功返回200
print len(response1.read())#打印读取网页长度 print'方法 2'
request=urllib2.Request(url)
request.add_header("user-agent","Mozilla/5.0")#伪装成浏览器
response2=urllib2.urlopen(request)
print response2.getcode()#验证打开网页是否成功,成功返回200
print len(response2.read())#打印读取网页长度 print '方法 3'
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3=urllib2.urlopen(url)
print response3.getcode()#验证打开网页是否成功,成功返回200
print cj
print len(response3.read())#打印读取网页长度
使用urllib2打开网页的三种方法(Python2)的更多相关文章
- 使用urllib2打开网页的三种方法
#coding:utf-8 import urllib2 import cookielib url="http://www.baidu.com" print '方法 1' resp ...
- urllib2下载网页的三种方法
1.最直接的方法 #-*- coding: utf-8 -*- import urllib2 #直接请求 response = urllib2.urlopen('https://www.baidu.c ...
- (转)C#调用默认浏览器打开网页的几种方法
转载,原文地址:http://blog.csdn.net/testcs_dn/article/details/42246969 CSharp调用默认浏览器打开网页的几种方法 示例界面: 方法一:从注册 ...
- Openerp 中打开 URL 的三种 方法
来自:http://shine-it.net/index.php/topic,8013.0.html 最近总结了,Openerp 中打开 URL 的三种 方法: 一.在form view 添加 < ...
- url下载网页的三种方法
# -*- coding: utf-8 -*- import cookielib import urllib2 url = "http://www.baidu.com" print ...
- 在Silverlight中打开网页的几种方法
HtmlPage.PopupWindow HtmlPopupWindowOptions option = new HtmlPopupWindowOptions(); option.Directorie ...
- python打开浏览器的三种方法
1.startfile方法 import os os.startfile("C:\Program Files (x86)\Google\Chrome\Application\chrome.e ...
- WPF中打开网页的两种方法
1.浏览器打开 Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "http://www. ...
- C#调用默认浏览器打开网页的几种方法
private void button1_Click(object sender, EventArgs e) { //从注册表中读取默认浏览器可执行文件路径 RegistryKey key = Reg ...
随机推荐
- HTTP协议header头域
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内 容请参考RFC2616.HTTP协议采用了请求/响应模型.客 ...
- Js删除Table中的一行
<html> <head> <title></title> <script type="text/javascript"> ...
- Linux多线程编程-信号量
在Linux中.信号量API有两组.一组是多进程编程中的System V IPC信号量.另外一组是我们要讨论的POSIX信号量. 这两组接口类似,但不保证互换.POSIX信号量函数都已sem_开头,并 ...
- 两段用来启动/重启Linux下Tomcat的Perl脚本
两段代码,第二段比较好些. 下面是Split输出结果方式的代码: #!/usr/local/bin/perl #Date:2015-07-07 print "Begin to restart ...
- 雕刻效果的实现【OpenCV+QT】
雕刻能够区分为凸雕和凹雕. 凸雕基右下角的点减去左上角的点. 凹雕是左上角的点减去右下角的点. [效果图] 由于进行了缩放.效果看起来差一些.
- vue2.0 引用qrcode.js实现获取改变二维码的样式
vue代码 <template> <div class="qart"> <div id="qrcode" ref="qr ...
- 移动端去掉按钮button默认样式
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- android权限申请Permission
代码地址如下:http://www.demodashi.com/demo/12432.html android在6.0系统以后,权限申请变得麻烦起来,今天介绍一个超级好用的权限申请库,我在使用中经过再 ...
- Struts2学习八----------接收参数
© 版权声明:本文为博主原创文章,转载请注明出处 接收参数 - 使用Action的属性接收参数 - 使用Domain Model接收参数 - 使用ModelDriven接收参数 实例 1.项目结构 2 ...
- Enumerate Combination C(k, n) in a bitset
Suppose n<=32, we can enumerate C(k, n), with bits representing absence or presence, in the follo ...