Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

执行download出现报错. #77

Open
xieguanru opened this issue Apr 19, 2018 · 4 comments
Open

执行download出现报错. #77

xieguanru opened this issue Apr 19, 2018 · 4 comments

Comments

@xieguanru
Copy link

xieguanru commented Apr 19, 2018

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
Copy link
Author

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

#这里需要判断一下返回内容的类型.
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
Copy link
Owner

ly0 commented Apr 24, 2018

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

@Michshelle
Copy link

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

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

@xieguanru
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants