使用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 ...
随机推荐
- iOS UI Element Usage
Bars The Status Bar
- 处理类型(typedef,uisng,auto,decltype)
一:类型别名是一个名字,它是某种类型的定价.有两种方法定义类型别名: 1.使用typedef关键字,如: typedef int *Int_Ptr Int_Ptr p=nullptr; //Int ...
- MapWindowPoints
中文名 MapWindowPoints Windows CE 1.0及以上版本 头文件 winuser.h 库文件 user32.lib MapWindowPoints函数把相对于一个窗口的坐标空间的 ...
- Vue框架引入JS库的正确姿势
参考自:https://mp.weixin.qq.com/s?src=3×tamp=1527154113&ver=1&signature=tWGeTa86gyK*RL ...
- STL源代码分析--第二级空间配置器
本文解说SGI STL空间配置器的第二级配置器. 相比第一级配置器,第二级配置器多了一些机制,避免小额区块造成内存的碎片.不不过碎片的问题,配置时的额外负担也是一个大问题.由于区块越小,额外负担所占的 ...
- ZOJ - 3890 Wumpus(BFS基础题)
Wumpus Time Limit: 2 Seconds Memory Limit: 65536 KB One day Leon finds a very classic game call ...
- Java Swing界面编程(25)---事件处理:鼠标事件及监听处理
假设想对一个鼠标的操作进行监听,假设鼠标按下.松开等.则能够使用MouseListener接口. package com.beyole.util; import java.awt.event.Mous ...
- C++里面定时器的使用
说白了就是三个函数的使用: SetTimer(20, 20, 0); //第一个20表示此定时器的标识符,第二个20表示你要定的时间,第三个不用管,设0即可. void CLMS511_interfa ...
- UML--组件图,部署图
组件图用于实现代码之间的物理结构,详细来说,就是实现代码交互.通过接口,将不同的软件,程序连接在一起. [理解] 1.组件的定义相当广泛,包含:源码,子系统,动态链接库,Activex控件. 2.组件 ...
- android历史
Android一词最早是出如今法国作家维里耶德利尔·亚当1986年发表的<未来夏娃>这部科幻小说中,作者利尔·亚当将外表像人类的机器起名为Android.这就是Android小人名字的由来 ...