Git Product home page Git Product logo

xhs's Introduction

🍰xhs

PyPI

Warning

The primary purpose of this repository is to practice my Python skills. It is important to note that web crawling may be considered illegal, and therefore, it is crucial to refrain from exerting any pressure or engaging in unauthorized activities on websites.

xhs is a crawling tool designed to extract data from xiaohongshu website

Usage

xhs is available on PyPI:

$ python -m pip install xhs

if you want the latest version, you can install from git:

$ python -m pip install git+https://github.com/ReaJason/xhs

Basic Usage

maybe now is more complex, Thanks @NanmiCoder

Please find in the document

xhs's People

Contributors

gyco avatar reajason avatar sam5440 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xhs's Issues

请问web端获取的cookie方式,我使用扫码登录后没有web_session字段

我使用example目录下的basic_usage.py
登录小红书web版,获取cookie并复制到代码中
程序逻辑在core.py 129行self.activate处报错,检测后发现我得cookie没有web_session字段

    def cookie(self, cookie: str):
        update_session_cookies_from_cookie(self.__session, cookie)
        if "web_session" not in self.cookie_dict:
            self.activate()

是不是有请求频率限制

获取笔记数据时,获取几十条就会出现频率限制
Traceback (most recent call last): File "/home/python3.9/lib/python3.9/site-packages/flask/app.py", line 2190, in wsgi_app response = self.full_dispatch_request() File "/home/python3.9/lib/python3.9/site-packages/flask/app.py", line 1486, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/python3.9/lib/python3.9/site-packages/flask/app.py", line 1484, in full_dispatch_request rv = self.dispatch_request() File "/home/python3.9/lib/python3.9/site-packages/flask/app.py", line 1469, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "/www/wwwroot/xhs_python/app.py", line 27, in xhs client = XhsClient(cookie) File "/home/python3.9/lib/python3.9/site-packages/xhs/core.py", line 114, in __init__ self.cookie = cookie File "/home/python3.9/lib/python3.9/site-packages/xhs/core.py", line 124, in cookie self.activate() File "/home/python3.9/lib/python3.9/site-packages/xhs/core.py", line 568, in activate return self.post(uri, data={}) File "/home/python3.9/lib/python3.9/site-packages/xhs/core.py", line 171, in post return self.request(method="POST", url=f"{self._host}{uri}", File "/home/python3.9/lib/python3.9/site-packages/xhs/core.py", line 156, in request raise IPBlockError(self.IP_ERROR_STR) xhs.exception.IPBlockError: 网络连接异常,请检查网络设置或重启试试

sign函数问题

现在内置的sign函数算出来的x-s长度和web网站的长度不一致,请求的时候一直返回 {'code': -1, 'success': False}

请问作者现在还维护吗?

PySide6 用户界面开发

  • 支持 cookie 登录、短信登录以及二维码登录
  • 支持指定笔记爬取笔记内容以及用户评论
  • 支持支持爬取用户所有笔记详情
  • 支持笔记评论自动回复
  • 博主笔记更新提醒
  • 根据关键字爬取笔记详情

basic_usage does not work as of 08/25/23

I used to be able to run basic_usage.py. But since yesterday, it does not work anymore. and I get
xhs.exception.DataFetchError: {'code': -1, 'success': False}
不确定是不是小红书那侧做了一些修改导致xhs不可用了

浏览器异常,请尝试关闭/卸载风险插件或重启试试!

from xhs import XhsClient

cookie = "mycookie"
xhs_client = XhsClient(cookie, user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
xhs_client.save_files_from_note_id("64467ef40000000013012007",
r"C:\Users\COMPUTER\Desktop\bot")

Traceback (most recent call last):
File "c:\Users\COMPUTER\Desktop\bot\tempCodeRunnerFile.python", line 4, in
xhs_client = XhsClient(cookie, user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")
File "C:\Users\COMPUTER\AppData\Local\Programs\Python\Python310\lib\site-packages\xhs\core.py", line 116, in init
self.cookie = cookie
File "C:\Users\COMPUTER\AppData\Local\Programs\Python\Python310\lib\site-packages\xhs\core.py", line 126, in cookie
self.activate()
File "C:\Users\COMPUTER\AppData\Local\Programs\Python\Python310\lib\site-packages\xhs\core.py", line 573, in activate
return self.post(uri, data={})
File "C:\Users\COMPUTER\AppData\Local\Programs\Python\Python310\lib\site-packages\xhs\core.py", line 176, in post
return self.request(method="POST", url=f"{self._host}{uri}",
File "C:\Users\COMPUTER\AppData\Local\Programs\Python\Python310\lib\site-packages\xhs\core.py", line 163, in request
raise DataFetchError(data.get("msg", None))
xhs.exception.DataFetchError: 浏览器异常,请尝试关闭/卸载风险插件或重启试试!

请问如何正确获取cookie

你好,我比较小白。。请见谅。
我是从52论坛来的,你开发的reatool软件,扫码登陆很好用,但我想试试cookie登录。
我把开发者模式-网络下找到的cookie值粘贴到软件里好像没用,二维码一直在刷新。
image
感觉是我没有正确获取cookie,请教一下该怎么获取。
谢谢!

'NoneType' object is not callable

使用的是python3.9
一开始还以为是我cookie的问题,换了好几次cookie还是报错

from xhs import XhsClient

cookie="a1=1886132f6cav7mfdpcid2mnbahv45uuvea5z3lvji30000329042;webId=b360cce06e1bcad5e48dafb7b612265e;gid=yYYKyqJiYydqyYYKyqJiK01hS0hW6ifUS3fJ2V36ID0xhuq84277hq888qJj84J8ji8dW2S0;gid.sign=3gVA5Ji2C13wAa8svTrYyqcJDc8=;webBuild=2.11.5;acw_tc=0ab967f98018c20dd6b0745ee8a1596c3750bf84d2f8727a93f5c10315054162; web_session=040069b253793fdd9ccd5842a2364b86586515;galaxy_creator_session_id=S5mv0vWIWqrCX2GSJK9LFaudFrUkNq7TYW3b;galaxy.creator.beaker.session.id=1687620188771003075230;xsecappid=creator-creator;websectiga=2a3d3ea002e7d92b5c9743590ebd24010cf3710ff3af8029153751e41a6af4a3;sec_poison_id=f93e48b6-3162-4f6f-9984-5fcd908695cb"
xhs_client=XhsClient(cookie)
# xhs_client.get_home_feed(xhs.FeedType.RECOMMEND)
xhs_client.get_note_by_id("63db8819000000001a01ead1")
Traceback (most recent call last):
  File "/Users/cyc/Desktop/小红书素材/run.py", line 6, in <module>
    xhs_client.get_note_by_id("63db8819000000001a01ead1")
  File "/usr/local/lib/python3.9/site-packages/xhs/core.py", line 190, in get_note_by_id
    res = self.post(uri, data)
  File "/usr/local/lib/python3.9/site-packages/xhs/core.py", line 176, in post
    self._pre_headers(uri, data, is_creator=is_creator)
  File "/usr/local/lib/python3.9/site-packages/xhs/core.py", line 148, in _pre_headers
    self.sign(url, data, a1=self.cookie_dict.get("a1"),
TypeError: 'NoneType' object is not callable

辛苦回复一下 感谢🙏🏻

请问一下这是什么问题导致的?

Traceback (most recent call last):
File "/home/one/myhome/path/to/xhs/test.py", line 61, in
a=xhs_client.get_user_all_notes(value)
File "/home/one/.local/lib/python3.10/site-packages/xhs/core.py", line 411, in get_user_all_notes
note = self.get_note_by_id(note_id)
File "/home/one/.local/lib/python3.10/site-packages/xhs/core.py", line 190, in get_note_by_id
res = self.post(uri, data)
File "/home/one/.local/lib/python3.10/site-packages/xhs/core.py", line 178, in post
return self.request(method="POST", url=f"{self._host}{uri}",
File "/home/one/.local/lib/python3.10/site-packages/xhs/core.py", line 165, in request
raise DataFetchError(data.get("msg", data))
xhs.exception.DataFetchError: 当前内容无法展示

发布笔记

目前发布笔记写了个简单的 case,但是发出去的图文无法正常显示图片,可能还需要其他操作什么的,暂时还没找到解决思路,先抛出来

def test_create_note_with_simple_desc(xhs_client: XhsClient):

下图为笔记详情图:

初始化client后,调用get_user_notes方法提示无登录信息,或登录信息为空

按照文档执行如下代码,提示“无登录信息,或登录信息为空”

from xhs import XhsClient

// cookie在网页https://www.xiaohongshu.com/user/profile/用户ID 
// 执行js代码:console.log(document.cookie); 得到,

cookie = "请从网页端获取你的 cookie" 
xhs_client = XhsClient(cookie)


result = xhs_client.get_user_notes("用户ID")
print(result)

执行效果如图:

image

请问如何能正常使用,谢谢🙏

get_user_notes无法获取所有的用户笔记

用 “xhs_client.get_user_notes("用户ID")” 这个api获取用户笔记时,无法获取全

返回的json数据包含了当前游标cursor和has_more来确认是否还有更多笔记以及请求更多笔记数量的偏移量,

但是您并没有提供处理笔记游标cursor的api

image

playwright._impl._api_types.Error: Execution context was destroyed, most likely because of a navigation

脚本中只修改了 cokkie,设置了a1,webID,web_session三个参数,有以下报错:
python example/basic_usage.py
Traceback (most recent call last):
File "/Users/echooo/Documents/Code/my_repo/周末计划生成器/xhs/example/basic_usage.py", line 50, in
note_info = xhs_client.get_note_by_id("64c27f08000000001701967a")
File "/opt/homebrew/lib/python3.9/site-packages/xhs/core.py", line 201, in get_note_by_id
res = self.post(uri, data)
File "/opt/homebrew/lib/python3.9/site-packages/xhs/core.py", line 187, in post
self._pre_headers(uri, data, is_creator=is_creator)
File "/opt/homebrew/lib/python3.9/site-packages/xhs/core.py", line 155, in _pre_headers
self.sign(
File "/Users/echooo/Documents/Code/my_repo/周末计划生成器/xhs/example/basic_usage.py", line 31, in sign
encrypt_params = context_page.evaluate(
File "/opt/homebrew/lib/python3.9/site-packages/playwright/sync_api/_generated.py", line 8698, in evaluate
self._sync(
File "/opt/homebrew/lib/python3.9/site-packages/playwright/_impl/_sync_base.py", line 109, in _sync
return task.result()
File "/opt/homebrew/lib/python3.9/site-packages/playwright/_impl/_page.py", line 395, in evaluate
return await self._main_frame.evaluate(expression, arg)
File "/opt/homebrew/lib/python3.9/site-packages/playwright/_impl/_frame.py", line 277, in evaluate
await self._channel.send(
File "/opt/homebrew/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 61, in send
return await self._connection.wrap_api_call(
File "/opt/homebrew/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 482, in wrap_api_call
return await cb()
File "/opt/homebrew/lib/python3.9/site-packages/playwright/_impl/_connection.py", line 97, in inner_send
result = next(iter(done)).result()
playwright._impl._api_types.Error: Execution context was destroyed, most likely because of a navigation

使用求助

感谢大佬开发的工具,使用时遇到了一个小问题,当获取博主所有笔记时,软件获取列表会获取一部分,然后下载时会下载列表内的一部分,例如共100篇,获取时获取了70,下载时下载了50。不知道是不是共性问题,希望大佬有时间可以查看一下,附博主号“5b13f01511be104e943a48ba”、或“5a6ec20cb1da1434c660a82d”

多账号体系

以下是我遇到的另外一个问题, 希望得到大佬的一些建议跟帮助

如果我管理的是一批的账号, 我发现 basic_sign_server.py 在启动之后只能为一个账号提供服务。

在启动 flask 时,通过 playwright = sync_playwright().start() ,playwright 只能被全局实例化一次。 导致这个服务只能为一个账号提供参数生成服务。

代码示例如下:

import requests
from xhs import XhsClient


def sign(uri, data=None, a1="", web_session=""):
    # 填写自己的 flask 签名服务端口地址
    res = requests.post("http://127.0.0.1:5006",
                        json={"uri": uri, "data": data, "a1": a1, "web_session": web_session})
    signs = res.json()
    return {
        "x-s": signs["x-s"],
        "x-t": signs["x-t"]
    }


def run(phone, cookie):
    # 实例化小红书客户端
    xhs_client = XhsClient(cookie, sign=sign)
    note_info = xhs_client.get_note_by_id("63db8819000000001a01ead1")
    print(note_info)


if __name__ == '__main__':
    cookie_1 = '''abRequestId=1; a1=18a4fb739bfi5fv56sf2afk6abyu0vzs146xvegob50000303745; webId=6cca84a5b1403b5bf6c91116bfc6cc0e; gid=yY04iDWqi8ydyY04iDWqj4dkDi32ih2KMiJ0KlDiTK0DvC2878huMh888q8qW428KiSSyd0J; webBuild=3.7.1; xsecappid=xhs-pc-web; web_session=030037a3c2c32f7f4ea717d189234a7620827d; websectiga=cffd9dcea65962b05ab048ac76962acee933d26157113bb213105a116241fa6c; sec_poison_id=c9818550-d81c-48c2-a28e-4d44a15efd38'''
    cookie_2 = '''imestamp2=166892735640546fac749a852dbc5491b1bce6bf3116f372b149e46ea126eea; timestamp2.sig=e6AlCiNVwsBa0vp8V7_-NlwkR4FNCYUxm8WvibiTqxg; smidV2=202211201455567df56e0e34e99881500da7e02dd59ddc00bfe477f456168a0; gid.ss=gSMQ9UOnDuZwH2oRGJG6BW6e4grs67TaYpnrW+8Wmd3BXEQ7tNY/0XN700Ebb+IN; xhsTrackerId=73749829-fb1c-4f59-8017-f401f1ef1f95; xhsTrackerId.sig=BfS4OY4--lw8f_3iEF2V5K83Qy8bCKOeIeJG1D2-ZZw; xsecappid=xhs-pc-web; a1=186056ada1a89mvl181dbjb8arvhnl4oy26cw5vxs50000249889; webId=2b0dc450f3eea2de6a1460b70f06983c; gid=yYK82K0ffWvJyYK82K0f0J2hy0Yj6h1yYyfDKCMVDY0FhS28xI14lE888J4jYYj8fY0Y2YK2; gid.sign=IvHoU2YKjJwV3p71N5/Jc1tbZ3k=; abRequestId=2b0dc450f3eea2de6a1460b70f06983c; websectiga=10f9a40ba454a07755a08f27ef8194c53637eba4551cf9751c009d9afb564467; sec_poison_id=d4fa3381-179d-41cc-ac42-555de878e0b8; cache_feeds=[]; web_session=0400698de7e997088ffbd819c4364b0f707729; webBuild=2.11.10'''
    accounts = [
        {"name": "user1", "phone": "1562604xxxx", "cookie": cookie_2},
        {"name": "user2", "phone": "1762973xxxx", "cookie": cookie_1},
    ]
    for account in accounts:
        name = account.get("name")
        phone = account.get("phone")
        cookie = account.get("cookie")
        print(name, phone)
        run(phone, cookie)

此时只有一个用户的执行正常。

请问,有没有办法获取关注的人数呢。

我想把我关注的都爬了,但一个个分享到电脑我要死的,大概500个,大佬有没有办法破解一下,我知道我是强人锁男,就问一下😭主要我硬盘炸了,又没有备份,之前都是一个个分享到桌面端。太惨了😭

请求频率问题

大佬你好,想问下推荐的接口和通过关键词搜索的接口多少秒请求一次才不会被封呢?

feat: 提供 web api 形式

似乎抓取用户笔记的需求比较多,考虑通过 cli 来实现抓取,免去使用者写代码的步骤

X-s 算法已更新

好像是昨天 X-s 不再走 sign 函数,而是走向了混沌的海洋。

image

跳进去就蒙蔽了

image

有点难以理解了,求大佬指导帮助,可以来这儿讨论:Q群668689006

更新之后一直显示“xhs.exception.DataFetchError: {'code': -1, 'success': False}”

之前有时会显示“当前内容无法展示”,昨天更新了一下一直报错:
auth_info = xhs_client.get_user_info(line)
File "C:\python\lib\site-packages\xhs\core.py", line 315, in get_user_info
return self.get(uri, params)
File "C:\python\lib\site-packages\xhs\core.py", line 173, in get
return self.request(method="GET", url=f"{self._host}{final_uri}")
File "C:\python\lib\site-packages\xhs\core.py", line 165, in request
raise DataFetchError(data.get("msg", data))
xhs.exception.DataFetchError: {'code': -1, 'success': False}

请帮助

About Save video or image

I installed the latest version: 0.1.9.
When I try to download files by note id, xhs client will exit with exception:

XhsClient' object has no attribute '_get_img_urls_from_note

I searched from source code, class XhsClient has no protect function _get_video_url_from_note and _get_img_urls_from_note

Should I intall any other requirements?

登录流程实现

你好,感谢项目提供的巨大帮助!

请问目前是否计划实现自动登录的流程, 用户端的操作,仅是扫二维码、收短信等, 不直接操作管理 cookie。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.