在整个面试过程中,笔试往往不是重点,但从一笔试可以看出一个人平时对基础知识的积累。
再说明的一点是,对于下面的问题,有的要求用php,有的要求java,但我用python实现的。很多时候公司并不是在意你必须用哪种语言去实现,语言只是工具,用来解决问题了,关键是否有思路。
验=证=邮=箱=格=式

验=证=邮=箱的格式,不同语言的实现大同小异,通过正则表达式是最快捷的匹配方式,但对于不熟悉正则的同学看着一长串匹配符还是比较头痛的,其实也没那么恐怖。
熟悉python 中正则表达式的常用个匹配符
先看一下=邮=箱=的一般格式:
x@x.x
x 表示一个或多个字符或数字。
1)第一个x可以字母数字
2)第二个x可以字母数字
3)第二个x可以字母,如.com,.cn,.net...等结尾
“@”和“.” 把内x拆成三部份。
整个邮箱长度最少等于5个字符。
代码如下:
#coding=utf-8
import re
'''
[a-zA-Z0-9] 匹配大小写字母与数字
[a-zA-Z] 匹配大小写字母
\@ a\@b a@b (字符转义)
\. a\.b a.b (字符转义)
'''
def emails(e):
if len(e)>= 5:
if re.match(\"[a-zA-Z0-9]+\@+[a-zA-Z0-9]+\.+[a-zA-Z]\",e) !=None:
return '邮=箱=格式正确!
'
return '邮=箱=格式有误'
e = raw_input(\"请输入email:\")
print e
a = emails(e)
print a
运行结果:
>>> ================================ RESTART ================================>>> 请输入email:1dfsdf2@22.2212@22.22邮箱格式有误>>> ================================ RESTART ================================>>> 请输入email:xsdfsdx@xdsfsx.comabzzzzxxxxc@126.com邮箱格式正确!
>>> ================================ RESTART ================================>>> 请输入email:邮箱格式正确!
.......
获得一个URL地址的扩展名
如:
address= 'sadf/lsdkjflsdkj/sllls/232323.html'的扩展名为html
对于这个问题同样使用正则式来解决
import re
def strings(url):
listt = ['.php','.html','.asp','.jsp']
for lis in listt:
suffix = re.findall(lis,url)
if len(suffix)>0:
return lis
address= 'sadf/lsdkjflsdkj/sllls/232323.html'
a = strings(url)
print a
运行结果:
.html
获得当前时间的前一天(或前一秒)
如果当前时间为:2014-6-11 17:12:45
前一天为:2014-6-10 17:12:45
前一秒为:2014-6-11 17:12:44
#coding=utf-8
import time
import datetime
#打印当前时间
print time.ctime()
#当前时间
now_time = datetime.datetime.now()
print now_time
#昨天的现在
yesterday = now_time + datetime.timedelta(days = -1)
print yesterday
#现在的前一秒
now_old = now_time + datetime.timedelta(seconds = -1)
print now_old
运行结果:
Wed Jun 11 17:21:07 20142014-06-11 17:21:07.7500002014-06-10 17:21:07.7500002014-06-11 17:21:06.750000
======
我也写了很多其他的非常简单的入门级的爬虫详细教程,
关注后,点击我的头像,就可以查看到。
欢迎大家一起留言讨论和交流,谢谢!