ReaJason / xhs

基于小红书 Web 端进行的请求封装。https://reajason.github.io/xhs/
MIT License
1.03k stars 295 forks source link

开发者你好,这个cookie是指下面图这里吗? #25

Closed ranyong1997 closed 1 year ago

ranyong1997 commented 1 year ago

image

ranyong1997 commented 1 year ago

我也尝试用web_session 也报错了 image

Esword618 commented 1 year ago

我也想知道

ReaJason commented 1 year ago

不好意思各位,可以使用 git pip 一下最新的。 python -m pip install git+https://github.com/ReaJason/xhs

这个是 cookie 的 key 没做 trim 去除空格导致的,或者可以自行将分号和字母前的空格删除如 a1=xxx; web_session=xxx; 改为 a1=xxx;web_session=xxx;

huyhi commented 1 year ago

正缺设置 cookie 后(登陆小红书,在浏览器中获取 cookie)。请求还是报错,trace如下。debug 了一下发现服务端返回的 http 状态码是 406,请问这是什么原因呢。

Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1496, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/hongye/workspace/ppd/xhs-demo/main.py", line 49, in notes = xhs_client.get_note_by_keyword("小红书") File "/Users/hongye/workspace/ppd/xhs-demo/venv/lib/python3.9/site-packages/xhs/core.py", line 367, in get_note_by_keyword return self.post(uri, data) File "/Users/hongye/workspace/ppd/xhs-demo/venv/lib/python3.9/site-packages/xhs/core.py", line 176, in post return self.request(method="POST", url=f"{self._host}{uri}", File "/Users/hongye/workspace/ppd/xhs-demo/venv/lib/python3.9/site-packages/xhs/core.py", line 163, in request raise DataFetchError(data.get("msg", None)) xhs.exception.DataFetchError: None

ReaJason commented 1 year ago

正缺设置 cookie 后(登陆小红书,在浏览器中获取 cookie)。请求还是报错,trace如下。debug 了一下发现服务端返回的 http 状态码是 406,请问这是什么原因呢。

Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1496, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/hongye/workspace/ppd/xhs-demo/main.py", line 49, in notes = xhs_client.get_note_by_keyword("小红书") File "/Users/hongye/workspace/ppd/xhs-demo/venv/lib/python3.9/site-packages/xhs/core.py", line 367, in get_note_by_keyword return self.post(uri, data) File "/Users/hongye/workspace/ppd/xhs-demo/venv/lib/python3.9/site-packages/xhs/core.py", line 176, in post return self.request(method="POST", url=f"{self._host}{uri}", File "/Users/hongye/workspace/ppd/xhs-demo/venv/lib/python3.9/site-packages/xhs/core.py", line 163, in request raise DataFetchError(data.get("msg", None)) xhs.exception.DataFetchError: None

不太清楚为什么会请求出错,本地测试是好着的。

image

我在代码中添加了,raise response,如果还出现你这个报错会打印响应内容 https://github.com/ReaJason/xhs/blob/b02b87f2d936358b01aabd7ab77a88f19e5489aa/xhs/core.py#L165 麻烦更新一下再试试,python -m pip install git+https://github.com/ReaJason/xhs,谢谢反馈💐 嗷对了,小红书对代理敏感,尽量不要开代理使用