python基础知识——基于python3.6
语法糖
# # -*- coding: utf-8 -*- # #------------- # #--------- 语法糖--------------- # #----------------------------------- # def a(fun): # print('aaaaaa') # fun() # print('bbbbb') # return ( # # ) # @a # def fun(): # print('hello')#要注入的
正则match
# 搜索 #--------------------- # import re # point = re.match('dd','www.nuaud.nadn') # if point is not None: # print(point.span()) # else: # print('no') # import re
用正则表达式搜索8位或11位手机号
------------------------------ #电话号码 #--------------------------- # point = re.search('^[0-9]{8}$|^[0-9]{11}$', '125667889999') # if point is not None: # print(point.span()) # else: # print('no') #------------------
用正则表达式查找日期
#查找日期 #----------------------------- # 2017-03-05 # import re # point = re.search('^[0-9]{4}-[0-1]{0,1}-[0-3]{0,1}-[0-9]{1}', '125667889999') # if point is not None: # print(point.span()) # else: # print('no')
get方式
#get方式 #-------------------------- # import urllib.request # f=urllib.request.urlopen("http://m.cnblogs.com/") # s=f.read() # print(s) #------------
正则提取
#正则提取 # #-------------------------- # import re # from urllib.request import urlopen # f = urlopen("http://www.meishij.net/") # s = f.read() # s=s.decode('utf-8') # mm = re.findall('<a href=\"(.*)\">(.*)</a>',str(s)) # print((mm)) # print((mm)[0][1])
爬虫爬一个网站
#爬招聘网站------------------------------ #--------------------------------- # import re # from urllib.request import urlopen # from urllib.request import Request # headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} # url = "http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%E5%8C%97%E4%BA%AC&kw=python&sm=0&p=1" # req_timeout = 5#______________________________--------------添加模拟浏览器协议头 # req = Request(url=url,headers=headers) # f = urlopen(req,None,req_timeout) # s = f.read() # s = s.decode('utf-8') # mm = str(s)
用代码新建一个文件,并写入txt文档
#新建file 写入txt—————————————————— #———————————————— # f=open("file/test.txt",'w') # f.write("First line 1.\n" ) # f.write("First line 2.\n" ) # f.write("First line 3.\n")