新年伊始-开年记

随波逐流,还是不停的瞎折腾~

新年伊始

过年来上班第一天就发现,SSR又登不上了,也就意味着,google啊,又离我远去。。。搞啥啊,查了下黄历,没啥问题啊,翻了下新华社新闻,好吧,开会了。。不出所料,被墙了。

周二时候有点不服,把vultr上几乎所有VPS都重搭了下,好吧,居然IP全挂,这么多台都全挂,有点服。。
今天早上过来,感觉还是有点不爽,主要是每次打开浏览器,由于我设置的Chrome主页就是google,每次进来都是空荡荡的,感觉很难受,抱着不死就折腾的心态,又开始搭,结果还真找到个漏网之鱼,苍天啊,真的是泪流满面o(╥﹏╥)o

立刻重建,还好有这个经历:

VPS重建记

一台新机器,花了半个小时就完成了,终于,又可以科学上网,hexo又可以更博了!!

日常记忆

随手记下昨天遇到的小白问题:
在搭建自动化框架时,基本会把页面元素的定位信息放在类似于example.ini的配置文件中,然后从文件中获取定位方式以及定位表达式,这样方便维护。
读取example.ini文件配置内容,我是使用的configparser,用法:

1
2
cf = configparser.ConfigParser()
cf.read('example.ini')

这样就会把配置文件中的配置信息加载到内存中,获取信息一般使用两种方式:

获取特定的optionValue

使用cf.get(section, option)即可获取特定的option的值
例如,有一个example.ini内容如下:

1
2
3
4
5
[126mail_login]
loginPage.frame = id > x-URS-iframe
loginPage.username = xpath > //input[@name="email"]
loginPage.password = xpath > //input[@name="password"]
loginPage.loginbutton = id > dologin

想要获取切换到登录frame的定位方式及表达式,只需要:

1
2
frame126 = cf.get("126mail_login", "loginPage.frame")
print(frame126)

打印出定位方式及表达式:

1
id > x-URS-iframe

获取指定section下的所有option

使用cf.items(section)即可获取该section下的所有option,返回的是一个列表,包含option以及对应的value
示例:

1
2
options = cf.items("126mail_login")
print(options)

返回结果:

1
[('loginpage.frame', 'id > x-URS-iframe'), ('loginpage.username', 'xpath > //input[@name="email"]'), ('loginpage.password', 'xpath > //input[@name="password"]'), ('loginpage.loginbutton', 'id > dologin')]

问题

由于日常的开发环境是windows,系统的默认编码是gbk,所有在windows下使用configparser来读取配置文件时,有的时候会报编码错误:

1
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 272: illegal multibyte sequence
解决

解决方法很简单,直接看cf.read()的源码,源码是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def read(self, filenames, encoding=None):
"""Read and parse a filename or a list of filenames.

Files that cannot be opened are silently ignored; this is
designed so that you can specify a list of potential
configuration file locations (e.g. current directory, user's
home directory, systemwide directory), and all existing
configuration files in the list will be read. A single
filename may also be given.

Return list of successfully read files.
"""
if isinstance(filenames, str):
filenames = [filenames]
read_ok = []
for filename in filenames:
try:
with open(filename, encoding=encoding) as fp:
self._read(fp, filename)
except OSError:
continue
read_ok.append(filename)
return read_ok

默认的read()是没有指定编码的encoding=None,所以只需要指定编码为utf-8即可解决:

1
cf.read(pageElementLocatorPath, encoding='utf-8')

其中pageElementLocatorPath为example.ini文件

后记

生活不就是不断的折腾嘛,墙了继续试,大不了你再墙嘛~

文章目录
  1. 新年伊始
  2. 日常记忆
    1. 获取特定的optionValue
    2. 获取指定section下的所有option
    3. 问题
      1. 解决
  3. 后记
|