#-*-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. 阿里云服务器ubuntu安装java运行环境

    服务器 阿里云服务器ubuntu安装java运行环境 转:http://www.codingyun.com/article/45.html 今天来给大家介绍一下在阿里云ubuntu服务器下安装java ...

  2. 分享一下我写的.net 2.0的orm类,实现mvc。可以用于webform等环境中,这是orm的原理部分。

    using System;using System.Collections.Generic;using System.Configuration;using System.Data;using Sys ...

  3. AngularJS 作用域与数据绑定机制

    AngularJS 简介 AngularJS 是由 Google 发起的一款开源的前端 MVC 脚本框架,既适合做普通 WEB 应用也可以做 SPA(单页面应用,所有的用户操作都在一个页面中完成).与 ...

  4. 水(NOIP模拟赛Round #10)

    题目描述: 小Z有一个长度为的数列.他有次令人窒息的操作,每次操作可以使某个数字或.他当然是希望这些数字的乘积尽量小了.为了简化题目,你只需输出操作完成后的数列即可. ———————————————— ...

  5. 杭电oj2032、2040、2042、2054、2055

    2032  杨辉三角 #include <stdio.h> int main(){ ][],i,j,n; while(~scanf("%d",&n)){ ;i& ...

  6. Vim文字编辑

    首先说明发现的vim编辑器的一个特点:vim编辑只有按[ENTER]键或命令模式下[o]才会换行,否则虽然在vim编辑器里显示的内容换行了,但事实上没有换行.如果你发现自己测试的效果和下面描述的不符, ...

  7. Centos7更改网卡名为eth0

    1.先更该网卡配置文件设备名和网卡名参数: vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 NAME=eth0DEVICE=eth0 2.将配 ...

  8. [thinkphp] 如何解析ajaxReturn返回的json字符串

    success: function(data){ var dataObj=eval("("+data+")");//转换为json对象 alert(dataOb ...

  9. Python的程序结构[2] -> 类/Class[2] -> 方法解析顺序 MRO

    方法解析顺序 / MRO (Method Resolution Order) 关于方法解析顺序(MRO)的详细内容可以参考文末链接,这里主要对 MRO 进行简要的总结说明以及一些练习示例. 经典类和新 ...

  10. (转)NSArray类的使用

    NSArray的基本应用总结 NSString *string1 = @"two"; NSString *string2 = @"one"; //创建数组 NS ...