python3 urllib.request 网络请求操作

基本的网络请求示例

  1. '''
  2. Created on 2014年4月22日
  3.  
  4. @author: dev.keke@gmail.com
  5. '''
  6. import urllib.request
  7.  
  8. #请求百度网页
  9. resu = urllib.request.urlopen('http://www.baidu.com', data = None, timeout = 10)
  10. print(resu.read(300))
  11.  
  12. #指定编码请求
  13. with urllib.request.urlopen('http://www.baidu.com') as resu:
  14. print(resu.read(300).decode('GBK'))
  15.  
  16. #指定编码请求
  17. f = urllib.request.urlopen('http://www.baidu.com')
  18. print(f.read(100).decode('utf-8'))

发送数据请求,CGI程序处理

  1. >>> import urllib.request
  2. >>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
  3. ... data=b'This data is passed to stdin of the CGI')
  4. >>> f = urllib.request.urlopen(req)
  5. >>> print(f.read().decode('utf-8'))
  6. Got Data: "This data is passed to stdin of the CGI"

PUT请求

  1. import urllib.request
  2. DATA=b'some data'
  3. req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
  4. f = urllib.request.urlopen(req)
  5. print(f.status)
  6. print(f.reason)

基本的HTTP验证,登录请求

  1. import urllib.request
  2. # Create an OpenerDirector with support for Basic HTTP Authentication...
  3. auth_handler = urllib.request.HTTPBasicAuthHandler()
  4. auth_handler.add_password(realm='PDQ Application',
  5. uri='https://mahler:8092/site-updates.py',
  6. user='klem',
  7. passwd='kadidd!ehopper')
  8. opener = urllib.request.build_opener(auth_handler)
  9. # ...and install it globally so it can be used with urlopen.
  10. urllib.request.install_opener(opener)
  11. urllib.request.urlopen('http://www.example.com/login.html')

支持代理方式验证请求

  1. proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
  2. proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
  3. proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
  4.  
  5. opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
  6. # This time, rather than install the OpenerDirector, we use it directly:
  7. opener.open('http://www.example.com/login.html')

添加 http headers

  1. import urllib.request
  2. req = urllib.request.Request('http://www.example.com/')
  3. req.add_header('Referer', 'http://www.python.org/')
  4. r = urllib.request.urlopen(req)

添加 user-agent

  1. import urllib.request
  2. opener = urllib.request.build_opener()
  3. opener.addheaders = [('User-agent', 'Mozilla/5.0')]
  4. opener.open('http://www.example.com/')

带参数的GET 请求

  1. >>> import urllib.request
  2. >>> import urllib.parse
  3. >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
  4. >>> f = urllib.request.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
  5. >>> print(f.read().decode('utf-8'))

带参数的POST请求

  1. >>> import urllib.request
  2. >>> import urllib.parse
  3. >>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
  4. >>> data = data.encode('utf-8')
  5. >>> request = urllib.request.Request("http://requestb.in/xrbl82xr")
  6. >>> # adding charset parameter to the Content-Type header.
  7. >>> request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
  8. >>> f = urllib.request.urlopen(request, data)
  9. >>> print(f.read().decode('utf-8'))

指定代理方式请求

  1. >>> import urllib.request
  2. >>> proxies = {'http': 'http://proxy.example.com:8080/'}
  3. >>> opener = urllib.request.FancyURLopener(proxies)
  4. >>> f = opener.open("http://www.python.org")
  5. >>> f.read().decode('utf-8')

无添加代理

  1. >>> import urllib.request
  2. >>> opener = urllib.request.FancyURLopener({})
  3. >>> f = opener.open("http://www.python.org/")
  4. >>> f.read().decode('utf-8')

http://www.cnblogs.com/cocoajin/p/3679821.html

【转】python3 urllib.request 网络请求操作的更多相关文章

  1. python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...

  2. Python3 urllib.request库的基本使用

    Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...

  3. python3 http.client 网络请求

    python3 http.client 网络请求 一:get 请求 ''' Created on 2014年4月21日 @author: dev.keke@gmail.com ''' import h ...

  4. 爬虫小探-Python3 urllib.request获取页面数据

    使用Python3 urllib.request中的Requests()和urlopen()方法获取页面源码,并用re正则进行正则匹配查找需要的数据. #forex.py#coding:utf-8 ' ...

  5. python 学习笔记之手把手讲解如何使用原生的 urllib 发送网络请求

    urllib.urlopen(url[,data[,proxies]]) : https://docs.python.org/2/library/urllib.html python 中默认自带的网络 ...

  6. (转)python3 urllib.request.urlopen() 错误UnicodeEncodeError: 'ascii' codec can't encode characters

    代码内容: url = 'https://movie.douban.com/j/search_subjects?type=movie'+ str(tag) + '&sort=recommend ...

  7. Hbuilder MUI里面使用java.net.URL发送网络请求,操作cookie

    1. 引入所需网络请求类: var URL = plus.android.importClass("java.net.URL"); var URLConnection = plus ...

  8. python3 urllib.request.urlopen() 地址打开错误

    错误内容:UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in ran ...

  9. 【dart学习】-- Dart之网络请求操作

    Flutter的请求网络有多种方式,一种是使用dart io中的HttpClient发起的请求,一种是使用dio库,另一种是使用http库,先学一下get和post,put.delete就等后面用到在 ...

随机推荐

  1. 复习ACCESS注入

    0x00前言:在学校看完了ACCESS注入.但当时并没有电脑,所以做好了笔记 回到家自己搭建了一个有ACCESS注入的站进行练习,虽然这可能没有什么用处 毕竟现在大多的网站都有waf或安全狗.而且AC ...

  2. 源码实现 --> strcmp

    比较字符串大小 函数 int strcmp(const char *string1, const char *string2); 比较字符串string1和string2大小. 返回值< 0,  ...

  3. Algorithm --> 投资组和求最大利润

    投资组和求最大利润 题目: 投资人出资一笔费用mount,投资给不同的公司(A,B,C....),求最大获取利润? 例如:投资400百万,给出两家公司A和B: 1.如果投资一百万给A公司,投资3百万给 ...

  4. Python -- Records项目学习

    Records学习笔记 Records链接地址 1. __getitem__(self, key) 内建方法(Build-in) 例子: class Test(object): def __getit ...

  5. Android类参考---SQLiteOpenHelper

    public 抽象类 SQLiteOpenHelper 继承关系 java.lang.Object |____android.database.sqlite.SQLiteOpenHelper 类概要 ...

  6. oracle中事务处理--事务隔离级别

    概念:隔离级别定义了事务与事务之间的隔离程度. ANSI/ISO SQL92标准定义了一些数据库操作的隔离级别(这是国际标准化组织定义的一个标准而以,不同的数据库在实现时有所不同). 隔离级别 脏读 ...

  7. 后台返回null iOS

    1.第一种解决方案 就是在每一个 可能传回null 的地方 使用  if([object isEqual:[NSNUll null]]) 去判断 2.第二种解决方案 网上传说老外写了一个Categor ...

  8. Unity发布WebGL时如何修改默认的载入进度条

    Unity发布WebGL版本后,需要去除Unity的Logo,首先关闭Splash Image去除Made with Unity启动画面(在File->Build Settings->Pl ...

  9. [福大软工] W班 软工实践原型设计—成绩公布

    作业地址 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/909 作业要求 详见作业地址 存在问题 1. ...

  10. beta冲刺3

    一,昨天的问题: 页面整理还没做 我的社团这边的后台数据库未完成,前端代码修改未完成. 二,今天已完成 页面整理基本完成,把登陆独立出来了,然后基本处理掉了多余页面(反正也没几个--) 我的社团这边试 ...