Skip to content

Commit

Permalink
docs: add cookie tips
Browse files Browse the repository at this point in the history
  • Loading branch information
ReaJason committed Aug 26, 2023
1 parent a448fe2 commit f2e1168
Showing 1 changed file with 4 additions and 135 deletions.
139 changes: 4 additions & 135 deletions docs/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,9 @@
基础使用
-----------
脚本例子看参考:`basic_usage.py <https://github.com/ReaJason/xhs/blob/master/example/basic_usage.py>`_
请注意 cookie 的获取,a1、web_session 和 webId 三个字段为必需字段。

.. code-block:: python3
from time import sleep
from xhs import XhsClient
from playwright.sync_api import sync_playwright
def get_context_page(instance, stealth_js_path):
chromium = instance.chromium
browser = chromium.launch(headless=True)
context = browser.new_context(
viewport={"width": 1920, "height": 1080},
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 "
"Safari/537.36"
)
context.add_init_script(path=stealth_js_path)
page = context.new_page()
return context, page
def sign(uri, data=None, a1="", web_session=""):
context_page.goto("https://www.xiaohongshu.com")
cookie_list = browser_context.cookies()
web_session_cookie = list(filter(lambda cookie: cookie["name"] == "web_session", cookie_list))
if not web_session_cookie:
browser_context.add_cookies([
{'name': 'web_session', 'value': web_session, 'domain': ".xiaohongshu.com", 'path': "/"},
{'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}]
)
sleep(1)
encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data])
return {
"x-s": encrypt_params["X-s"],
"x-t": str(encrypt_params["X-t"])
}
if __name__ == '__main__':
cookie = "please get cookie from your website"
stealth_js_path = "your downloaded stealth.min.js file path"
playwright = sync_playwright().start()
browser_context, context_page = get_context_page(playwright, stealth_js_path)
xhs_client = XhsClient(cookie, sign=sign)
# get note info
note_info = xhs_client.get_note_by_id("63db8819000000001a01ead1")
print(note_info)
# resource release
playwright.stop()
具体代码参考:`basic_usage.py <https://github.com/ReaJason/xhs/blob/master/example/basic_usage.py>`_


进阶使用
Expand All @@ -96,91 +45,11 @@
开启 Flask 签名服务
^^^^^^^^^^^^^^^^^^^^^^^^
脚本地址: `basic_sign_server <https://github.com/ReaJason/xhs/blob/master/example/basic_sign_server.py>`_

.. code-block:: python3
from flask import Flask, request
from playwright.sync_api import sync_playwright
from gevent import monkey
monkey.patch_all()
app = Flask(__name__)
def get_context_page(instance, stealth_js_path):
chromium = instance.chromium
browser = chromium.launch(headless=True)
context = browser.new_context(
viewport={"width": 1920, "height": 1080},
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 "
"Safari/537.36"
)
context.add_init_script(path=stealth_js_path)
page = context.new_page()
return context, page
# 如下更改为 stealth.min.js 文件路径地址
stealth_js_path = "/Users/reajason/ReaJason/xhs/tests/stealth.min.js"
playwright = sync_playwright().start()
browser_context, context_page = get_context_page(playwright, stealth_js_path)
context_page.goto("https://www.xiaohongshu.com")
def sign(uri, data, a1, web_session):
browser_context.add_cookies([
{'name': 'web_session', 'value': web_session, 'domain': ".xiaohongshu.com", 'path': "/"},
{'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}]
)
encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data])
return {
"x-s": encrypt_params["X-s"],
"x-t": str(encrypt_params["X-t"])
}
@app.route("/", methods=["POST"])
def hello_world():
json = request.json
uri = json["uri"]
data = json["data"]
a1 = json["a1"]
web_session = json["web_session"]
return sign(uri, data, a1, web_session)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5005)
具体代码参考: `basic_sign_server <https://github.com/ReaJason/xhs/blob/master/example/basic_sign_server.py>`_


使用 XhsClient
^^^^^^^^^^^^^^^^^^^
第一次请求会失败,但是之后的请求就正常了。

脚本地址: `basic_sign_usage <https://github.com/ReaJason/xhs/blob/master/example/basic_sign_usage.py>`_

.. code-block:: python3
import requests
from xhs import XhsClient
def sign(uri, data=None, a1="", web_session=""):
# 填写自己的 flask 签名服务端口地址
res = requests.post("http://localhost:5005",
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"]
}
if __name__ == '__main__':
cookie = "please get cookie from your website"
xhs_client = XhsClient(cookie, sign=sign)
# get note info
note_info = xhs_client.get_note_by_id("63db8819000000001a01ead1")
print(note_info)
具体代码参考: `basic_sign_usage <https://github.com/ReaJason/xhs/blob/master/example/basic_sign_usage.py>`_

0 comments on commit f2e1168

Please sign in to comment.