zoukankan      html  css  js  c++  java
  • urllib,urllib2,requests对比

      1 #coding:utf-8
      2 import urllib2
      3 import urllib
      4 import httplib
      5 import socket
      6 import requests
      7 
      8 #实现以下几个方面内容:
      9 ##get请求,post请求
     10 ##请求参数自定义(querystring 针对get,form针对post,cookie,header)
     11 ##返回内容格式
     12 ##实现代理
     13 def testforurllib():
     14     r=urllib.urlopen('http://www.baidu.com')
     15     #返回的内容
     16     r.readline()
     17     r.read()
     18     r.info()
     19     r.getcode()
     20     r.geturl()
     21     #get 加参数
     22     params=urllib.urlencode({'name':'yy','age':22})#结果:name=yy$age=22
     23     r1=urllib.urlopen('http://www.baidu.com?%s'%params)
     24     #post 加参数
     25     r2=urllib.urlopen('http://www.baidu.com',params)
     26     print(r2.getcode())
     27     #代理
     28     proxies = {'http': 'http://127.0.0.1:7070/'}
     29     opener=urllib.FancyURLopener(proxies)
     30     opener.open('http://www.baidu.com')
     31     print(opener.getcode())
     32     #cookie实现比较没找到好的方法
     33     pass
     34 def testforurllib2():
     35     #代理
     36     proxy=urllib2.ProxyHandler({'http':'http://127.0.0.1:7070'})
     37     opener=urllib2.build_opener(proxy)
     38     #局部
     39     opener.open('http://baidu.com')
     40     ##全局
     41     urllib2.install_opener(opener)
     42     urllib2.urlopen('bakdu.com')
     43     
     44     #get
     45     urllib2.urlopen('http://cnblogs.com?%s'%urllib.urlencode({'page':2}))
     46     #post
     47     urllib2.urlopen('http://cnblogs.com',urllib.urlencode({'page':2}))
     48     
     49     #cookie
     50     import cookielib
     51     cj=cookielib.CookieJar()
     52     opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
     53     r=opener.open('http://weibo.com')
     54     print(r.info())
     55     #定制http头
     56     r=urllib2.Request('http://cnblogs.com')
     57     r.add_header('user-agent','xxx')
     58     response=urllib2.urlopen(r)
     59     pass
     60 def testforhttplib():
     61     #urllib是对httplib的封装,如果没有更精细的控制,使用urllib即可
     62     #http://www.cnblogs.com/qq78292959/archive/2013/04/01/2993133.html
     63     url='http://cnblogs.com'
     64     params={'page':1}
     65     
     66 def testforrequests():
     67     #这个api设置更爽
     68     url='http://www.baidu.com'
     69     params={'page':1}
     70     r=requests.get(url)
     71     r1=requests.post('http://httpbin.org/post')
     72     #同理有put,delete,head,options
     73     #添加参数
     74     r3=requests.get(url,params=params)
     75 
     76     #获取响应
     77     r4=requests.get('https://github.com/timeline.json')
     78     print(r4.text+r4.encoding+str(r4.raw))
     79 
     80     #添加post的data数据
     81     import json
     82     r5=requests.post('http://baidu.com',data={'page':1})
     83     print(r5.status_code)
     84 
     85     #添加http头
     86     headers={'a':'a'}
     87     r6=requests.post('http://baidu.com',headers=headers)
     88     print(r6.headers)
     89 
     90     #添加cookie
     91     c=dict(a='a')
     92     r7=requests.get('http://baidu.com',cookies=c)
     93     print(len(r7.cookies))
     94 
     95     #响应内容
     96     r7.text
     97     r7.content
     98     r7.json()
     99     r7.raw
    100 
    101     r7.status_code
    102     r7.headers
    103     r7.cookie['key']
    104     r7.history
    105     pass
    106 
    107 
    108 def main():
    109     testforrequests()
    110     pass
    111 
    112 
    113 main()
    View Code

    综上所述还是requests的api更好理解,使用起来也更简洁。

  • 相关阅读:
    JMeter 参数化
    Jmeter 录制脚本(二)
    转:JMeter压力测试及并发量计算
    转:系统的平均并发用户数和并发数峰值如何估算
    Jmeter 录制脚本(一)
    用mfix模拟流化床时压力边界条件和迭代步长需要注意的问题
    Everything开机自启
    用fluent模拟内循环床气化燃烧(调试过程记录)
    ubuntu配置
    shell 命令合并文本
  • 原文地址:https://www.cnblogs.com/cncyber/p/4444368.html
Copyright © 2011-2022 走看看