math
数学运算的一系列函数
import math #导入库
dir(math) #查看库中包含的函数
help(math.radians) # 查看某个具体函数的帮助
OS
模块 与系统交互的模块
import os #导入库
dir(os) #查看库中包含的函数
help(os.chdir) # 查看某个具体函数的帮助
random
模块 提供随机功能
import random
random.choice(['c++','java','python'])
'c++'
random.randint(1,100)
96
random.randrange(0,10,2)
8
random.random()
0.8494016173444663
random.uniform(5,10)
6.736951630571202
random.sample(range(101),10)
[2, 81, 42, 24, 72, 4, 34, 39, 98, 36]
nums=[1,2,3,4,5]
random.shuffle(nums)
nums
[5, 2, 4, 1, 3]
datetime
模块 提供和时间有关的模块
import datetime
from datetime import date
date.today()
datetime.date(2019, 9, 23)
from datetime import time
tm=time(19,36,25)
print(tm)
19:36:25
from datetime import datetime
datetime.now()
datetime.datetime(2019, 9, 23, 9, 32, 12, 486622)
print(datetime.now().strftime('%A,%b %d %Y %H:%M'))
Monday,Sep 23 2019 09:33
dt=datetime(2019,9,23,9,33)
ts=dt.timestamp()
ts
1569202380.0
print(datetime.fromtimestamp(ts))
2019-09-23 09:33:00
查看异常类 dir(__builtins__)
前几个以Error
结尾的
try except finally else
处理机制
try:
raise
except [Exception as err]:
print(err)
finally:
do
else:
do
例子 可以有多个except
语句 和Java
的异常区别就像循环里和存在的区别一样
try:
num1=int(input('enter the first number:'))
num2=int(input('enter the second number:'))
print(num1/num2)
except ValueError:
print('please input a digit')
except ZeroDivisionError as err:
print('The second number cannot be zero')
print(err)
else:
print('everything is ok')
try:
num1=int(input('enter the first number:'))
num2=int(input('enter the second number:'))
print(num1/num2)
except (ValueError,ZeroDivisionError):
print('Invalid input')
else:
print('everything is ok')
try:
num1=int(input('enter the first number:'))
num2=int(input('enter the second number:'))
print(num1/num2)
except:
print('something went wrong')
else:
print('everything is ok')
try:
num1=int(input('enter the first number:'))
num2=int(input('enter the second number:'))
print(num1/num2)
except Exception as err:
print('something went wrong')
print(err)
else:
print('everything is ok')
和循环结合
while True:
try:
num1=int(input('enter the first number:'))
num2=int(input('enter the second number:'))
print(num1/num2)
break
except Exception as err:
print('something went wrong')
print(err)
else:
print('everything is ok')
finally:
print('this circle is over')
with
语句close
)with open('入门0.ipynb') as f:
for line in f:
print(line,end=' ')
文件的打开 open
函数–返回一个可迭代的文件对象
file_obj=open(filename ,mpdel='r',buffering=-1,...)
使用help(open)
查看更多
model
为可选参数 默认值是r
基础是r w a
三个模式 加b
表示以二进制方式
buffering
也是可选参数 默认值是-1
(0表示不缓冲,>=1的值表示缓冲一行或者缓冲区大小
f1=open(r'/home/yang/test.txt') #默认以读方式打开
f1=open(r'/home/yang/test.txt','w') #以写方式打开 会删除源数据 谨慎
f1=open(r'/home/yang/test.txt','wb',0) #以二进制写的方式打开 谨慎
相关函数
写文件 file_obj.write(str)
with open('Desktop/tset.txt','w') as f:
f.write('hello world')
读文件 file_obj.read([size])
带参数表示至多读出size字节数据 返回字符串。不带则读到文件结束 返回字符串(会包含换行符)
with open('Desktop/tset.txt') as f:
p1=f.read(5)
p2=f.read()
print(p1,p2)
hello world
读写多行数据
with open('test.txt') as f1:
cNames=f1.readlines()
for i in range(0,len(cNames)):
cNames[i]=str(i+1)+' '+cNames[i]
with open('stest.txt','w') as f2:
f2.writelines(cNames)
文件指针 file_name.seek(offset,whence=0)
whence
(0表示文件头部 1表示当前位置 2表示文件尾部)移动offset
个字节whence
为可选参数 默认值为0(利用好相关库的官网提供的文档 注意所爬网站的爬虫协议)
抓取
urllib
内建模块
urllib.request
Requests
第三方库Scrapy
框架解析
BeautifulSoup
库re
模块例子 爬取分析豆瓣评书对《情书》的书评和评分
import requests
from bs4 import BeautifulSoup
import re
r=requests.get('https://book.douban.com/subject/30230525/comments/')
soup=BeautifulSoup(r.text,'lxml')
pattern=soup.find_all('span','short')
#for item in pattern:
# print(item.string)
pattern_s=re.compile('