#-*-coding:utf-8-*-
import urllib
import urllib2
import cookielib
##urllib
url="http://www.qq.com"
header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"}
def urllib_study(url):
urldata=urllib.urlopen(url)
print(urldata.read().decode('gbk'))##网页内容
print(urldata.getcode())##http状态码
print(urldata.geturl())## request url
print(urldata.info()) ##header info urldata.info().getparam('charset')获取header 编码
urllib.urlretrieve(url,'D:\\pic\\')##下载文件
def urlretrieve_study(url,savepath):
def callback(a,b,c):
down_progess=100.0*a*b/c
if down_progess>100:
down_progess=100
print '%.2f%%'%down_progess
urllib.urlretrieve(url,savepath,callback)
#urlretrieve_study(url,'D:\\pics\\qq.html')
##urllib2
def urllib2_study(url):
urldata=urllib2.urlopen(url)
 
#-*-coding:utf-8-*-
import urllib2
import random
url=""
useragent=["Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36", ]
def get_content(url,useragent):
"""
@
"""
random_header=random.choice(useragent)
req=urllib2.Request(url)
req.add_header('User-Agent',random_header)
req.add_header('Host',)
req.add_header("Refer",)
req.add_header('GET',)
ret=urllib2.urlopen(req).read()
return ret

  

print(dir(urldata))##urldata.read.decode('gbk') urldata.getcode() urldata.info() urldata.geturl()
cookie=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
data=opener.open(url)##urllib2.urlopen其实也是调用opener.open
print(data.read().decode('gbk'))
def urllib2_post(url):
values={'name':'howhy','age':32}
data=urllib.urlencode(values)
req=urllib2.Request(url,data,header)
response=urllib2.urlopen(req)
the_page=response.read()
def handler():##一般handler

    htp=urllib2.HTTPHandler()
opener=urllib2.build_opener(htp)
req=urllib2.Request("http://www.baidu.com/")
print(opener.open(req).read()) def proxy():##代理handler
hand=urllib2.ProxyHandler({"http":"219.141.153.41:80"})
opener=urllib2.build_opener(hand)
req=urllib2.Request("http://www.baidu.com/")
print(opener.open(req).read())
def authhandler():##基本认证handler
htppwd=urllib2.HTTPPasswordMgrWithDefaultRealm()
htppwd.add_password(None,"auth web","username","password")
htp=urllib2.HTTPBasicAuthHandler(htppwd)
opener=urllib2.build_opener(htp) ##此时可以加多个handler
opener.addheaders=[("User-Agent","dsffsdfdsfd"),()]
req=urllib2.Request("http://auth web")
print(opener.open(req).read())

urllib urllib2的更多相关文章

  1. python中urllib, urllib2,urllib3, httplib,httplib2, request的区别

    permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...

  2. python urllib urllib2

    区别 1) urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL.这意味着,用urllib时不可以伪装User Agent字符串等. 2) u ...

  3. httplib urllib urllib2 pycurl 比较

    最近网上面试看到了有关这方面的问题,由于近两个月这些库或多或少都用过,现在根据自己的经验和网上介绍来总结一下. httplib 实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更 ...

  4. python通过get方式,post方式发送http请求和接收http响应-urllib urllib2

    python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...

  5. python中 urllib, urllib2, httplib, httplib2 几个库的区别

    转载 摘要: 只用 python3, 只用 urllib 若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了 python2.X 有这些库名可用: urllib, urll ...

  6. 人生苦短之Python的urllib urllib2 requests

    在Python中涉及到URL请求相关的操作涉及到模块有urllib,urllib2,requests,其中urllib和urllib2是Python自带的HTTP访问标准库,requsets是第三方库 ...

  7. urllib,urllib2,requests对比

    #coding:utf-8 import urllib2 import urllib import httplib import socket import requests #实现以下几个方面内容: ...

  8. 【Python爬虫实战--1】深入理解urllib;urllib2;requests

    摘自:http://1oscar.github.io/blog/2015/07/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3urllib;urllib2;reques ...

  9. python2 处理urllib/urllib2错误并打印源码

    import urllib2 import urllib url = 'http://localhost/12.php' data = {} data['cmd']='whoami' data = u ...

随机推荐

  1. Angular(三)

    一.使用Module(模块)组织依赖关系 模块提供了一种方法,可以用来组织应用中一块功能区域的依赖关系:同时还提供了一种机制,可以自动解析依赖关系(又叫做依赖注入).一般来说,我们把这些叫做依赖服务, ...

  2. 自定义View Layout过程 (3)

    目录 目录 1. 知识基础 具体请看我写的另外一篇文章:(1)自定义View基础 - 最易懂的自定义View原理系列 2. 作用 计算View视图的位置. 即计算View的四个顶点位置:Left.To ...

  3. Instruments Tutorial for iOS: How To Debug Memory Leaks

    http://www.raywenderlich.com/2696/instruments-tutorial-for-ios-how-to-debug-memory-leaks Update 4/12 ...

  4. c++ poco StreamSocket tcpclient测试用例

    1.代码 #include <iostream> #include "Poco/Net/Socket.h" #include "Poco/Net/Stream ...

  5. DropZone图片上传控件的使用

    前台代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w ...

  6. mysql高可用架构之-MHA学习

    此博文参考  博主:mysql高级DBA yayun  完成 简介: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司 ...

  7. vue.js 2的表单控件

    静下心,抄一段sample,以后可以快点到这里来抄...: <!DOCTYPE html> <html> <head> <meta charset=" ...

  8. Ext中点击某个东西弹出框展示

    renderer:function(v){ return '<a href="123" data-qtitle="OK Button" data-qwid ...

  9. Java多线程设计模式(1)

    1 线程中的几个方法解释 Thread.sleep(long 毫秒) 在指定的时间内让当前线程休眠,将控制权交给其他线程,休眠的线程依旧拥有它所占用的锁, Thread.yield(); 暂停或者放弃 ...

  10. UBIFS

    转:http://www.armadeus.com/wiki/index.php?title=UBIFS This is a preliminary page dealing with the ins ...