2 25urllib.py
"""
urllib.request.urlopen(url,data,timeout)
"""
# from urllib.request import urlopen
# import urllib.parse
# from urllib.error import URLError
# import socket
# url = "http://httpbin.org/post"
# data = bytes(urllib.parse.urlencode({'name': 'dc'}), encoding="utf-8")
# try:
# reponse = urlopen(url = url, data=data, timeout=5)
# except URLError as e:
# if isinstance(e.reason,socket.timeout):
# print("TIME OUT")
# else:
# print(reponse.read().decode("utf-8"))
"""
urllib.request.Request(url,data,headers,method)
"""
# from urllib.request import Request,urlopen
# import urllib.parse
# url = "http://httpbin.org/post"
# data = bytes(urllib.parse.urlencode({'name': 'dc'}), encoding="utf-8")
# headers = {
# 'User-Agent': 'Mozilla/4.0(compatible;Msie5.5;Windows NT)'
# }
# req = Request(url=url, data=data, headers=headers, method="POST")
# reponse = urlopen(req)
# print(reponse.read().decode("utf-8"))
"""
Handler 验证 代理 Cookies build_opener
"""
"""
验证
"""
# from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener
# import urllib.error
# url = "https://www.zhihu.com/signup?next=%2F"
# name = "17380646919"
# value = "dc201637"
# p = HTTPPasswordMgrWithDefaultRealm()
# p.add_password(None, url, name, value)
# handler = HTTPBasicAuthHandler(p)
# opener = build_opener(handler) # try:
# reponse = opener.open(url)
# except urllib.error.URLError as e:
# print(e.reason)
# else:
# print(reponse.read().decode("utf-8"))
"""
代理
"""
# from urllib.error import URLError
# from urllib.request import ProxyHandler, build_opener
# url = "http://www.baidu.com"
# Proxy_Handler = ProxyHandler({
# 'http': 'http://127.0.0.1:9743',
# 'https':'https://127.0.0.1:9743'
# })
# opener = build_opener(Proxy_Handler)
# try:
# reponse = opener.open(url)
# print(reponse.read().decode("utf-8"))
# except URLError as e:
# print(e.reason)
"""
cookies
"""
"""
打印cookies的值
"""
# from urllib.request import HTTPCookieProcessor,build_opener
# import http.cookiejar
# url = "http://www.baidu.com"
# cookie = http.cookiejar.CookieJar()
# handler = HTTPCookieProcessor(cookie)
# opener = build_opener(handler)
# reponse = opener.open(url)
# for items in cookie:
# print(items.name + "=" + items.value)
"""
保存cookies的值
"""
# from urllib.request import HTTPCookieProcessor,build_opener
# import http.cookiejar
# url = "http://www.baidu.com"
# filename = "cookies.txt"
# # cookie = http.cookiejar.MozillaCookieJar(filename)
# cookie = http.cookiejar.LWPCookieJar(filename)
# handler = HTTPCookieProcessor(cookie)
# opener = build_opener(handler)
# reponse = opener.open(url)
# cookie.save(ignore_discard=True, ignore_expires=True)
"""
读取cookies的值并应用
"""
# from urllib.request import HTTPCookieProcessor,build_opener
# import http.cookiejar
# url = "http://www.baidu.com"
# cookie = http.cookiejar.LWPCookieJar()
# cookie.load('cookies.txt',ignore_discard=True,ignore_expires=True)
# handler = HTTPCookieProcessor(cookie)
# opener = build_opener(handler)
# reponse = opener.open(url)
# print(reponse.read().decode("utf-8"))
"""
异常处理 URLError、HTTPError
"""
# from urllib import error, request
# try:
# reponse = request.urlopen("http://cuiqingcai.com/index.htm")
# except error.HTTPError as e:
# print(e.reason,e.code,e.headers,sep= '\n')
# except error.URLError as e:
# print(e.reason)
# else:
# print('no worry')
"""
reson属性返回一个对象
"""
# import urllib.request
# import socket
# from urllib.error import HTTPError, URLError
# try:
# reponse = urllib.request.urlopen("https://www.baidu.com", timeout=0.1)
# except URLError as e:
# print(type(e.reason))
# if isinstance(e.reason, socket.timeout):
# print("TIMEOUT")
"""
解析链接
"""
2 25urllib.py的更多相关文章
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
- Python导入其他文件中的.py文件 即模块
import sys sys.path.append("路径") import .py文件
- import renumber.py in pymol
cp renumber.py /usr/local/lib/python2.7/dist-packages/pymol import renumber or run /path/to/renumber ...
- python gettitle.py
#!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...
- 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!
情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...
- caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例
caffe机器学习环境搭建及python接口编译参见我的上一篇博客:机器学习caffe环境搭建--redhat7.1和caffe的python接口编译 1.运行caffe图片分类器python接口 还 ...
- 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优
libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...
- MySqlNDB使用自带的ndb_setup.py安装集群
在用Mysql做集群时,使用Mysql的NDB版本更易于集群的扩展,稳定和数据的实时性. 我们可以使用Mysql自带的工具进行集群安装与管理:ndb_setup.py.位于Mysql的安装目录bin下 ...
- 将做好的py文件打包成模块,供别人安装调用
现在要将写完的3个py文件,打包. 步骤: 1.新建一个文件夹setup(名字随便取),在setup文件夹下,再新建一个文件夹financeapi. 2.将上面4个py文件拷贝至financeapi文 ...
随机推荐
- How To Secure Nginx with Let's Encrypt on Ubuntu 14.04
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14 ...
- .net 开源的网站
https://www.zhihu.com/question/24408855 https://www.zhihu.com/question/19840134 这两个知乎的答案 介绍部分经典的.net ...
- MongoDB在MFC下使用C++驱动编译错误的解决
.post p{text-indent: 2em;} 今天使用MongoDB的C++驱动,在编译连接的时候一直出现错误,显示的string_data.h下93行max宏的问题,可视其本身并不是调用ma ...
- js获取对象所有的keys
Js中获取对象的所有key值 假如现在有一个对象 var obj = { A:2 ,B:"Ray" ,C:true ,D:function(){} } 如果想遍历对象obj中的 ...
- 一篇RxJava友好的文章(三)
组合操作符 继上一篇讲述了过滤操作符,这一篇讲述组合操作符,组合操作符可用于组合多个Observable.组合操作符相对于过滤操作符要复杂很多,也较难以理解,需要花费时间去看文档查资料,写demo才能 ...
- mpvue项目中安装weui
观察一下发现,mpvue项目打包css的规律是:根组件App.vue里的style样式全部打包到 dist / static / css / app.wxss .. 参照微信小程序的原生引入使 ...
- 利用bootstrap实现图片Carousel效果
引入头文件: <link rel="stylesheet" href="bootstrap.min.css"> <link rel=" ...
- BZOJ3671: [Noi2014]随机数生成器(贪心)
Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 2098 Solved: 946[Submit][Status][Discuss] Descripti ...
- 基于 win7下虚拟机的 GNSS-SDR安装过程
最近在安装 GNSS-SDR软件时,遇到了很多问题,这里回顾了我的安装过程,罗列了所遇到的问题和解决办法.希望后来者不要再踩这些坑了! 首先,在官方文档中看到,GNSS-SDR目前并不支持直接在 Wi ...
- 【杂题总汇】NOIP2013(洛谷P1967) 货车运输
[洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...