python datetime的使用

datetime在Python中使用很频繁,发现有许多都需要去学习,所以今天就拿这个来做测试。这个就很好了。

时间转日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import datetime
log_date
week = datetime.datetime.strptime(log_date, "%Y-%m-%d").weekday()
if week == 0:
return '星期一'
elif week == 1:
return '星期二'
elif week == 2:
return '星期三'
elif week == 3:
return '星期四'
elif week == 4:
return '星期五'
elif week == 5:
return '星期六'
elif week == 6:
return '星期日'

时间字符串转时间

1
2
3
4
5
import datetime
start = '2020-04-04 01:00:00'

start_date = datetime.datetime.strptime(log_date, "%Y-%m-%d %H:%M:%S")

Supervisord 启动报错:No such file or directory: file: /usr/lib/python2.7/socket.py line: 224

最近在部署的时候遇到一个错误

1
2
error: , [Errno 2] No such file or directory: file: /usr/lib/python2.7/socket.py line: 224

如上,如果 supervisord 正常启动过,突然报这个错误,则有可能是非正常关闭导致的。
此时,需要强行关闭后,再正常启动即可。

1
ps aux | grep supervisord

首先用sudo kill -9 XXX关闭supervisord
然后关闭运营中的程序
然后启动

1
$ sudo /bin/supervisord -c /etc/supervisord/supervisord.conf

ssl.SSLError: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version错误

今天在写一个demo的时候,报了这个错误。代码如下:

1
2
3
4
5
import requests

r = requests.get("https://github.com/favicon.ico")
with open('favicon.ico', 'wb') as f:
f.write(r.content)

运行报错,错误消息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Traceback (most recent call last):
File "/Users/FQY/env350/lib/python3.5/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/Users/FQY/env350/lib/python3.5/site-packages/urllib3/connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "/Users/FQY/env350/lib/python3.5/site-packages/urllib3/connectionpool.py", line 849, in _validate_conn
conn.connect()
File "/Users/FQY/env350/lib/python3.5/site-packages/urllib3/connection.py", line 356, in connect
ssl_context=context)
File "/Users/FQY/env350/lib/python3.5/site-packages/urllib3/util/ssl_.py", line 359, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 376, in wrap_socket
_context=self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 747, in __init__
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 983, in do_handshake
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 628, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:646)

网上百度都说的是要升级pip或者python,因为我用的是python3.5,应该不是这个问题,所以应该升级openssl
升级

1
$ pip install pyopenssl 

这时候在运行就没问题了。

python encode和decode函数的用法

从英文意思上看,encode和decode分别指编码和解码。在python中,Unicode类型是作为编码的基础类型。

1
2
     encode                 decode
str ---------> Unicode ---------> str
encode的作用是将其他编码的字符串转换成unicode编码,如str1.encode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。 
decode的作用是将unicode编码转换成其他编码的字符串,如str2.decode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码

因此,转码的时候一定要先搞明白,字符串str是什么编码,然后decode成unicode,然后再encode成其他编码
下面咱们用几个例子来看下:

1
2
3
4
5
6
7
8
9
10
11
s = '中国,你好'
print('type is {}'.format(type(s)))
# 用encode进行编码
s1 = s.encode('utf-8')
print('s1 is {}'.format(s1))
print('type is {}'.format(type(s1)))
# 用decode进行编码
s2 = s1.decode('utf-8')
print('s2 is {}'.format(s2))
print('type is {}'.format(type(s2)))

运行结果是:

1
2
3
4
5
type is <class 'str'>
s1 is b'\xe4\xb8\xad\xe5\x9b\xbd\xef\xbc\x8c\xe4\xbd\xa0\xe5\xa5\xbd'
type is <class 'bytes'>
s2 is 中国,你好
type is <class 'str'>

看完运行结果大家就知道怎么用了。😆

爬虫系列之urllib的使用

urllib库是python爬虫中的常用库,这个库来说,我们不需要关心请求的链接是什么,需要传的参数是什么,以及如何设置可选的请求头就好了,不用深入到底层去了解它到底是怎样的传输和通信的。有了它,两行代码就可以完成一个请求和相应的处理过程,得到网页。

hexo 添加本地图片

hexo插入图片的方式有好几种,链接或者本地图片,介于链接图片有可能失效的原因,用本地图片是最省事的,目前插入本地图片有两种方式。

  • 跟随文章的创建图片文件夹
  • 创建总的图片文件夹