This commit is contained in:
熊潇 2025-04-29 18:08:15 +08:00
commit e1e232a2f5
6 changed files with 221 additions and 0 deletions

137
.gitignore vendored Normal file
View File

@ -0,0 +1,137 @@
venv
# Python cache files
__pycache__/
*.pyc
*.pyo
*.pyd
_PyCache__/
*.py[cod]
*$py.class
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
.coverage.*
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
site/
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# virtualenvwrapper project settings
.virtenv/
.virtualenv/
# VS Code settings
.vscode/settings.json
.vscode/settings.*

2
init.sh Normal file
View File

@ -0,0 +1,2 @@
pip install -r requirements.txt
playwright install chromium

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "@kevisual/social-xhs-api-server",
"scripts": {
"start": "node index.js"
}
}

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
playwright
xhs
gevent
requests

65
server.py Normal file
View File

@ -0,0 +1,65 @@
import time
from flask import Flask, request
from gevent import monkey
from playwright.sync_api import sync_playwright
monkey.patch_all()
app = Flask(__name__)
A1 = ""
def get_context_page(instance, stealth_js_path):
chromium = instance.chromium
browser = chromium.launch(headless=True)
context = browser.new_context()
context.add_init_script(path=stealth_js_path)
page = context.new_page()
return context, page
# 如下更改为 stealth.min.js 文件路径地址
stealth_js_path = "stealth.min.js"
print("正在启动 playwright")
playwright = sync_playwright().start()
browser_context, context_page = get_context_page(playwright, stealth_js_path)
context_page.goto("https://www.xiaohongshu.com")
print("正在跳转至小红书首页")
time.sleep(5)
context_page.reload()
time.sleep(1)
cookies = browser_context.cookies()
for cookie in cookies:
if cookie["name"] == "a1":
A1 = cookie["value"]
print("当前浏览器 cookie 中 a1 值为:" + cookie["value"] + ",请将需要使用的 a1 设置成一样方可签名成功")
print("跳转小红书首页成功,等待调用")
def sign(uri, data, a1, web_session):
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("/sign", 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)
@app.route("/a1", methods=["GET"])
def get_a1():
return {'a1': A1}
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5005)

7
stealth.min.js vendored Normal file

File diff suppressed because one or more lines are too long