爬虫相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

208 lines
8.8 KiB

import json
import time
from urllib.parse import urlparse
import requests
import execjs
from sing import get_auth
eval_js = execjs.compile(open(r'D:\2026-3\xhs\js\main.js', 'r', encoding='utf-8').read())
def get_a1():
a1 = eval_js.eval('generateLocalId()')
return a1
def get_x_s(url, data=None):
u = urlparse(url)
result = u.path
if u.query != "":
result += "?" + u.query
if data:
if type(data) == dict:
data = json.dumps(data, ensure_ascii=False)
result += data
x_s = eval_js.call('xhs_x_s', result)
return x_s
def get_common(a1, b1):
x_s = eval_js.call('get_common', a1, b1)
return x_s
def get_x_t():
return str(int(time.time() * 1000))
def get_headers(cookie, url, data=None):
headers = {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"authorization;": "",
"cache-control": "no-cache",
"pragma": "no-cache",
"priority": "u=1, i",
"referer": "https://creator.xiaohongshu.com/publish/publish?source=official&from=tab_switch",
"sec-ch-ua": "\"Chromium\";v=\"9\", \"Not?A_Brand\";v=\"8\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 SLBrowser/9.0.6.8151 SLBChan/10 SLBVPV/64-bit",
"x-s": get_x_s(url, data),
"x-s-common": get_common(cookie['a1'], ''),
"x-t": get_x_t(),
}
return headers
cookies = {
"a1": get_a1(),
"webId": "f2675311fd5ef2666e15073ef7876bb9",
"gid": "yjWf8S8JKJ7qyjWf8S8JJW6j2fJ0xSVhJ6fYvW61Y1A09828yWy69U888yy2jKY8YY0Yjf0i",
"x-user-id-ark.xiaohongshu.com": "6861bde9000000001e01790f",
"customerClientId": "706990196683713",
"x-user-id-fuwu.xiaohongshu.com": "6861bde9000000001e01790f",
"abRequestId": "f2675311fd5ef2666e15073ef7876bb9",
"web_session": "040069769f151fb684b60b65903b4b342673dc",
"id_token": "VjEAAApbUkDez1irEIDK8Xskk9WfmyF6jSocgzGzSSzuhxM1cdthEiup4YdxvB5E6aGLqvdW7DvcwGDBYIbfs0G2mejs1Wf5kYkdDCpD2bFo46Gu3EprnI8se/nm/lE80TPlBClU",
"customer-sso-sid": "68c517612540944882532354dbfxgoivhkkgdhc5",
"x-user-id-creator.xiaohongshu.com": "5a0457e4db2e6045e3f6d4e0",
"access-token-creator.xiaohongshu.com": "customer.creator.AT-68c517612540944882532355njwip1rjvzxfmmyi",
"galaxy_creator_session_id": "hMLkZfiS1vCMl6Ls1unn7AdCJN1EQuNkVHCZ",
"galaxy.creator.beaker.session.id": "1772432808364005045757",
"webBuild": "5.14.0",
"unread": "{%22ub%22:%2269a568c4000000001b01d337%22%2C%22ue%22:%22699edff00000000028008ccc%22%2C%22uc%22:25}",
"acw_tc": "0a0d096b17728479400813507e65716b1f67026c1556a667d2cbd8eef79b3a",
"xsecappid": "ugc",
"loadts": "1772847946657",
"websectiga": "9730ffafd96f2d09dc024760e253af6ab1feb0002827740b95a255ddf6847fc8",
"sec_poison_id": "e52f4379-6e82-42d0-8133-d806bcc4d280"
}
def get_file():
url = 'https://creator.xiaohongshu.com/api/media/v1/upload/creator/permit?biz_name=spectrum&scene=image&file_count=1&version=1&source=web'
response = requests.get(url, headers=get_headers(cookie=cookies, url=url), cookies=cookies)
return response.json()
def put_data(fileid, token):
with open('1.png', 'rb') as p:
data = p.read()
t = str(int(time.time()))
auth = get_auth({
"SecretId": "null",
"SecretKey": "null",
"Method": "PUT",
"Pathname": fileid,
"Query": {},
"Headers": {
"Content-Length": len(data),
"Host": "ros-upload.xiaohongshu.com"
},
"KeyTime": f"{t};{t}",
"UseRawKey": False
})
print(auth)
url = f'https://ros-upload.xiaohongshu.com/{fileid}'
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "zh-CN,zh;q=0.9",
"Authorization": auth,
"Cache-Control": "",
"Content-Type": "",
"Origin": "https://creator.xiaohongshu.com",
"Pragma": "no-cache",
"Priority": "u=1, i",
"Referer": "https://creator.xiaohongshu.com/",
"Sec-Ch-Ua": "\"Chromium\";v=\"9\", \"Not?A_Brand\";v=\"8\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 SLBrowser/9.0.6.8151 SLBChan/10 SLBVPV/64-bit",
"X-Cos-Security-Token": token
}
res = requests.put(url=url, headers=headers, data=data).text
print('-------------', res)
def new(file_id, title, connect):
url = "https://edith.xiaohongshu.com/web_api/sns/v2/note"
data = {
"common": {
"type": "normal",
"note_id": "",
"source": "{\"type\":\"web\",\"ids\":\"\",\"extraInfo\":\"{\\\"subType\\\":\\\"official\\\",\\\"systemId\\\":\\\"web\\\"}\"}",
"title": title,
"desc": connect,
"ats": [],
"hash_tag": [],
"business_binds": "{\"version\":1,\"noteId\":0,\"bizType\":0,\"noteOrderBind\":{},\"notePostTiming\":{},\"noteCollectionBind\":{\"id\":\"\"},\"noteSketchCollectionBind\":{\"id\":\"\"},\"coProduceBind\":{\"enable\":true},\"noteCopyBind\":{\"copyable\":true},\"interactionPermissionBind\":{\"commentPermission\":0},\"optionRelationList\":[]}",
"privacy_info": {
"op_type": 1,
"type": 0,
"user_ids": []
},
"goods_info": {},
"biz_relations": [],
"capa_trace_info": {
"contextJson": "{\"recommend_title\":{\"recommend_title_id\":\"\",\"is_use\":3,\"used_index\":-1},\"recommendTitle\":[],\"recommend_topics\":{\"used\":[]}}"
}
},
"image_info": {
"images": [
{
"file_id": file_id,
"width": 3442,
"height": 4590,
"metadata": {
"source": -1
},
"stickers": {
"version": 2,
"floating": []
},
"extra_info_json": "{\"mimeType\":\"image/png\",\"image_metadata\":{\"bg_color\":\"\",\"origin_size\":1282.61328125}}"
}
]
},
"video_info": None
}
# data_raw = '{"common":{"type":"normal","note_id":"","source":"{\\"type\\":\\"web\\",\\"ids\\":\\"\\",\\"extraInfo\\":\\"{\\\\\\"subType\\\\\\":\\\\\\"official\\\\\\",\\\\\\"systemId\\\\\\":\\\\\\"web\\\\\\"}\\"}","title":"111","desc":"222","ats":[],"hash_tag":[],"business_binds":"{\\"version\\":1,\\"noteId\\":0,\\"bizType\\":0,\\"noteOrderBind\\":{},\\"notePostTiming\\":{},\\"noteCollectionBind\\":{\\"id\\":\\"\\"},\\"noteSketchCollectionBind\\":{\\"id\\":\\"\\"},\\"coProduceBind\\":{\\"enable\\":true},\\"noteCopyBind\\":{\\"copyable\\":true},\\"interactionPermissionBind\\":{\\"commentPermission\\":0},\\"optionRelationList\\":[]}","privacy_info":{"op_type":1,"type":0,"user_ids":[]},"goods_info":{},"biz_relations":[],"capa_trace_info":{"contextJson":"{\\"recommend_title\\":{\\"recommend_title_id\\":\\"\\",\\"is_use\\":3,\\"used_index\\":-1},\\"recommendTitle\\":[],\\"recommend_topics\\":{\\"used\\":[]}}"}},"image_info":{"images":[{"file_id":"spectrum/KLpGPJCvUHX1KUGCjYgq2pG_pgnnovE6c-1snNb8CKYNOo8","width":3024,"height":4032,"metadata":{"source":-1},"stickers":{"version":2,"floating":[]},"extra_info_json":"{\\"mimeType\\":\\"image/png\\",\\"image_metadata\\":{\\"bg_color\\":\\"\\",\\"origin_size\\":1745.205078125}}"}]},"video_info":null}'
data = json.dumps(data, ensure_ascii=False)
headers = get_headers(cookies, url, data)
headers["Content-Type"] = "application/json;charset=UTF-8"
response = requests.post(url, headers=headers, cookies=cookies, data=data.encode("utf-8"))
print(response.text)
# def get_file_url():
# url = "https://www.xiaohongshu.com/web_api/sns/v5/creator/file/encryption?file_id=%2Fspectrum%2FQfyQUONtYxXAxO16o8dxSipox7U8UonPCJu9hvYHLq5AjrI&type=image&ts=1772869510861&sign=0f37b5816f1a65c43087554236326cf3"
# response = requests.get(url, headers=get_headers(cookies,url,), cookies=cookies, params=params)
#
# print(response.text)
# print(response)
def run():
file = get_file()
print(file)
fileid = file['data']['uploadTempPermits'][0]['fileIds'][0]
token = file['data']['uploadTempPermits'][0]['token']
put_data(fileid, token)
new(fileid, '我是标题', '内容内容')
if __name__ == '__main__':
run()