ly0 / baidupcsapi

百度网盘api
MIT License
1.21k stars 234 forks source link

执行download出现报错. #77

Open xieguanru opened 6 years ago

xieguanru commented 6 years ago

from baidupcsapi import PCS pcs = PCS(username, password) pcs.download("/abc.txt")

执行之后会报错: Traceback (most recent call last): File "/home/hehe/.local/lib/python3.5/site-packages/baidupcsapi/api.py", line 130, in wrapper foo = json.loads(ret.content.decode('utf-8')) File "/usr/lib/python3.5/json/init.py", line 319, in loads return _default_decoder.decode(s) File "/usr/lib/python3.5/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "t.py", line 5, in pcs.download('/recovery_key.txt') File "/home/hehe/.local/lib/python3.5/site-packages/baidupcsapi/api.py", line 1133, in download extra_params=params, **kwargs) File "/home/lalala/.local/lib/python3.5/site-packages/baidupcsapi/api.py", line 139, in wrapper raise LoginFailed('User unsigned in.') baidupcsapi.api.LoginFailed: User unsigned in.

trace了一下代码, 发现在 https://github.com/ly0/baidupcsapi/blob/master/baidupcsapi/api.py#L129

每一个请求都会json.loads一下,包括下载文件的那个请求 然而, 这次返回的是文件的内容, 并不是json 就会导致exception

请问这是个bug么?还是我的姿势不对?

xieguanru commented 6 years ago

问题已经解决, 发现几个问题, 周知一下

这里需要判断一下返回内容的类型.

https://github.com/ly0/baidupcsapi/blob/master/baidupcsapi/api.py#L127

if type(ret) == requests.Response and ret.headers['Content-Type'].lower().find('json') != -1:

还有, get_streaming返回的内容是H265(hevc)编码的,不要尝试用浏览器播放!

ly0 commented 6 years ago

用的是 download 方法吗?这个方法已经被标记为 deprecated 了,尝试使用 download_url ?

Michshelle commented 6 years ago

用pcs.download_url 还是相同问题。 pcs.downlaod_url('/abc.txt') baidupcsapi.api.LoginFailed: User unsigned in.

然后如果print(pcs.list_files('/').content) 可以读到百度盘上的内容。 这还能抢救么?谢谢!

xieguanru commented 6 years ago

@Michshelle 我上面已经给了解决办法, 你可以试一下 .