Python问题记录
-
urllib方法无法使用
原因:python3中不支持urllib2
解决:使用urllib.request来代替 -
TypeError: cannot use a string pttern on a bytes-like object
原因:数据格式出现错误
解决:将数据的格式转换一下,我这里是获取html数据,所以使用
数据.decode(‘utf-8’);转换格式 -
AttributeError: ‘NoneType’ object has no attribute ‘xpath’
原因:数据请求为空,无法进行xpath转换;可能是遇到反扒。
解决:需要再请求地址是,增加headers来解决。
headers去地址的请求信息那里获取User-Agent即可 -
FileNotFoundError: [Errno 2] No such file or directory : ‘ *.类型 ’
原因:文件找不到异常,可能是在查找文件时由于路径问题找不到相关文件
解决:检查open(fileName)中的fileName的格式;
fileName=绝对路径+‘/’+文件名.类型 -
TypeError: write() argument must be str, not bytes
原因:写入类型错误
解决:我开始用的是‘w’,输入格式是字符。而这边需要的是bytes二进制数据类型,所以用‘wb+’来写入 -
‘ascii’ codec can’t encode characters in position 76-77: ordinal not in range(128)
原因:请求的数据类型不匹配
解决:本来是用urllib.request.urlopen()来请求获取pdf文件的,可是格式不匹配,所以切换了requests.get()来获取 -
a bytes-like object is required, not ‘Response’
原因:写入类型错误
解决:write()中需要传响应数据中的二进制数据,不能直接将响应数据传入
错误:f.write(downloadFile) 注释:downloadFile为接口响应数据
正确:f.write(downloadFile.content) 注释:content为二进制数据 -
DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead. db[MONGO_TABLE].insert(data[i])
原因:python3不支持mongdb的insert()方法,被弃用了
解决:使用insert_one()或者insert_many -
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
原因:python3不支持mongdb的insert()方法,被弃用了
解决:使用insert_one()或者insert_many