<div style='--en-codeblock:true;--en-blockId:xTMsTumJ7jQ;--en-meta:{"title":"datetime引用方法","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>from datetime import date
#导入时间库
now=date.today()
#取当前时间
print(now)
birthday=date(1987,12,3)
print(birthday)
age=now-birthday
#假设年龄=当前日期-生日日期
print(age)</div>
<div style='--en-codeblock:true;--en-blockId:LhTQQCXshz9;--en-meta:{"title":"示例","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>import zlib
m = b'This is a test compress'
print(m)
m1=len(m)
#查看字符串的长度
print(m1)
t = zlib.compress(m)
#假设压缩后的内容为t
t1=len(t)
#查看压缩后内容t的长度
print(t)
print(t1)
s = zlib.decompress(t)
#解压缩后的内容为s
print(s)</div>
<div style='--en-codeblock:true;--en-blockId:OnD6SejKc-K;--en-meta:{"title":"示例","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'>import sys
a=sys.path
#假设系统路径为a
print(a)</div>
<div style='--en-codeblock:true;--en-blockId:wX2cR-NeJn_;--en-meta:{"title":"示例","lang":"Python","theme":"default","showLine":true,"lineWrap":false};box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial; margin-top: 6px;'># coding=UTF-8
import urllib
url = 'https://blog.csdn.net/alice_tl'
wp = urllib.urlopen(url)
file_content = wp.read()
print file_content
#第一部分为获取网页源码
fp = open('alice.txt', 'wb') #打开一个文本文件
fp.write(file_content) #写入数据
fp.close() #关闭文件
#第二部分为将网页内容存入文件中
#第三部分为利用正则表达式将文件内容打印出来
import re
fp = open('alice.txt', 'rb')
content = fp.read()
fp.close()
title = re.search('<title>(.*?)</title>', content, re.S).group(1)
print 'title = ', title + '\n'
hrefPatten = 'href="(.*?)"'
hrefC = re.findall(hrefPatten, content, re.S) #返回所有匹配正则表达式的值于列表中
print 'Allhref = ', hrefC
for h in hrefC :
print h</div>
💬 评论交流
这个技巧很实用,感谢分享!