一.%与format的区别
format是python2.6新增的⼀个格式化字符串的⽅法,相对于⽼版的%格式⽅
法,它有很多优点。
1.不需要理会数据类型的问题,在%⽅法中%s只能替代字符串类型
2.单个参数可以多次输出,参数顺序可以不相同
print(‘hello {0} i am {1} . my name is
{0}’.format(‘Kevin','Tom'))
二.urllib与urllib2的区别
urllib2可以接受⼀个Request类的实例来设置URL请求的headers,
urllib仅可以接受URL。这意味着,你不可以通过urllib模块伪装你的
User Agent字符串。
urllib提供urlencode⽅法⽤来GET查询字符串的产⽣,⽽urllib2没有。这是为何
urllib常和urllib2⼀起使⽤的原因
三. response.text与response.content的区别
response.text返回的类型是str
response.content返回的类型是bytes,可以通过decode()⽅法将
bytes类型转为str类型
推荐使⽤:response.content.decode()的⽅式获取相应的html⻚⾯
四. re/xpath/beautifulsoap需要导⼊哪些包
import re
from lxml import etree
from bs4 import BeautifulSoup
五.写一个装饰器,计算函数运行时间
import time
def outer(func):
def inner(*args,**kwargs):
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
print('运行时间是{0}'.format(end_time-start_time))
return inner
@outer
def test():
time.sleep(3)
test()
网友评论