1、在python3.3后urllib2已经不能再用,只能用urllib.request来代替。
2、with open(filename,‘w’)as file_object: ,我们要以写入模式打开这个文件。 打开文件时,可指定读取模式(‘r’)、写入模式(‘w’)、附加模式(‘a’)或者读写模式(’r+’)。(‘wb’)方式打开,写入字符串会报错,因为这种打开方式为:以二进制格式打开一个文件只用于写入。
filename='programming.txt'
with open(filename,'w') as file_object:
file_object.write ('I love programming.')
3、 python中platform(#平台)模块提供很多方法去获取操作系统的信息。如 platform.system()获取操作系统的类型。
4、urllib.request.urlopen(url),打开网页,urlopen()函数返回类文件对象,提供以下内建方法:
- read() , readline() ,readlines() , fileno() , close() :这些方法的使用方式与文件对象完全一样
- info():返回一个httplib.HTTPMessage对象,表示远程服务器返回的头信息
- getcode():返回Http状态码。
- geturl():返回请求的url
5、用python打开百度首页
import urllib.request
import time,platform,os
def clear():
'''该函数用于清屏'''
print('内容较多,显示3秒后翻页')
time.sleep(3)
OS=platform.system() #获取操作系统的类型
if(OS=='Windows'):
os.system('cls') #windows系统清屏
else:
os.system('clear') #linux系统清屏
def linkBaidu():
url='http://www.baidu.com'
try:
response=urllib.request.urlopen(url,timeout=3) #timeout防止访问时间过长造成假死
except urllib.request.URLError:
print('网络地址错误')
exit()
with open('./baidu.txt','wb') as fp: #这里要用‘wb’以二进制打开一个文件,只用于写入。 ./baidu.txt表示在同一目录下
fp.write(response.read()) #在txt文件中写入url获取的信息
print('获取url信息,response.geturl() \n:%s'%response.geturl())
print('获取返回代码,response.getcode() \n:%s'%response.getcode())
print('获取返回信息,response.info() \n:%s'%response.info())
print('获取的网页内容已存入当前目录的baidu.txt中,请自行查看')
if __name__=='__main__':
linkBaidu()
获取url信息,response.geturl()
:http://www.baidu.com
获取返回代码,response.getcode()
:200
获取返回信息,response.info()
:Date: Mon, 02 Apr 2018 08:53:46 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: Close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=40C6AC3CEC5BB25C88B3E2EAB103E33C:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=40C6AC3CEC5BB25C88B3E2EAB103E33C; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1522659226; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=1469_22534_13289_21101_18559_20930; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Cxy_all: baidu+9e1624c9e8fcfece3993f392e3bf6dec
Expires: Mon, 02 Apr 2018 08:53:03 GMT
X-Powered-By: HPHP
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
BDPAGETYPE: 1
BDQID: 0xa5c7ae9200027e12
BDUSERID: 0
获取的网页内容已存入当前目录的baidu.txt中,请自行查看
6、sys模块提供了一系列有关python运行环境的变量和函数。
sys.argv,获取当前正在执行的命令行参数的参数列表。
变量 | 解释 |
---|---|
sys.argv | 当前程序名 |
sys.argv[1] | 第一个参数 |
sys.argv[0] | 第二个参数 |
7、正则表达式re模块中,用re.compile()编译后,用re.match()或re.search()来匹配。其区别在于:
match()函数只检测RE是不是在string的开始位置匹配 search()会扫描整个string查找匹配,会扫描整个字符串并返回第一个成功的匹配 也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none 二、例子:match(): print(re.match(‘super’, ‘superstition’).span())会返回(0, 5) print(re.match(‘super’, ‘insuperable’))则返回None
search(): print(re.search(‘super’, ‘superstition’).span())返回(0, 5) print(re.search(‘super’, ‘insuperable’).span())返回(2, 7)
8、函数replace(),把字符串中的old(旧字符串)替换成new(新字符串),如果指定第三个参数max,则替换不超过max 次。
str.replace(old, new[, max])
.replace(old, new[, max])
9、