python学习笔记
来源:互联网 发布:哲学社会科学数据库 编辑:程序博客网 时间:2024/06/15 12:36
html=req.read().decode('gb18030')
# bytes object
b = b"example"# str object
s = "example"
# str to bytes
bytes(s, encoding = "utf8")
# bytes to str
str(b, encoding = "utf-8")
# an alternative method
# str to bytes
str.encode(s)
# bytes to str
bytes.decode(b)
import urllib2
content = urllib2.urlopen('http://XXXX').read()
代理服务器:
import urllib2
proxy_support = urllib2.ProxyHandler({'http':'http://XX.XX.XX.XX:XXXX'})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
content = urllib2.urlopen('http://XXXX').read()
cookie的处理:
import urllib2, cookielib
cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
content = urllib2.urlopen('http://XXXX').read()
同时使用代理和cookie;
opener = urllib2.build_opener(proxy_support, cookie_support, urllib2.HTTPHandler)
Python 一般字符串变量如何转化为 raw string
我看到大部分raw string都是通过对字符串常量前面加个r来定义,
有没有转化为raw string的函数呢?
如果我有个一般字符串变量,怎么才能把这个变量转为raw string?
找了半天,似乎没有这种需求,很奇怪
谢谢!
------解决方案--------------------------------------------------------
有些字符打不出来或显不出来,所以使用转义符方式让人看得明白是有个东西在那儿,但是这个是文本代码里的字符面值要变成内部数据时才发生转变,已经是内部数据就没再进行转义,也就没所谓raw不raw的区别。
如果因某种错误需要修复,可以试试用eval()或repr()处理一下...
>>> a = '\\123'
>>> print a
\123
>>> a = repr(a)[1:-1]
>>> print a
\\123
>>> a = eval("'%s'" % a)
>>> print a
\123
mport:
import http.client
因为HTTPConnection是在http.client下面的。因为装的是python3,猜想是新的python有些名字改了,在google上一找,果然如此,和http相关的有这几个对应关系:
httplib -> http.client
Cookie -> http.cookies
cookielib -> http.cookiejar
BaseHTTPServer -> http.server
SimpleHTTPServer -> http.server
CGIHttpServer -> http.server
这个地址是关于python2到python3 port的一些变化:
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html
- 【python】:python学习笔记
- Python学习笔记-Python起步
- Python学习笔记-Python基础
- Python学习笔记-Python对象
- Python基本概念--Python学习笔记
- Python学习笔记:Python函数
- python学习笔记-python安装
- Python学习笔记--Python基础
- 【Python】python基础学习笔记
- python学习笔记3
- Python学习笔记
- Boost.Python学习笔记
- python学习笔记1
- python学习笔记2
- python学习笔记(2)
- Python学习笔记 Module
- Python学习笔记.
- python学习笔记(1)
- uva 112 - Tree Summing
- 另类弹窗十倍放大流量
- 学习:10位IT领袖的10条道
- JavaScript弹出窗口以及窗口间的通信
- JAVA中的容器
- python学习笔记
- 基于msp430小车驱动,寻光设计,超声波测距
- DP【状态压缩 】 <简单?> hlg 1473
- sdwriter脚本分析
- Windows 8于10月26日正式发售:主要功能解析
- 模拟算法
- 【转】关于SAP的用户出口 SAP的用户功能增强
- hdu 1269
- ACE日志系统之C/S模式配置文件详解