小红书app复制链接转换为直接可访问链接,网页版链接,小红书短链转长链(最新版)
2026/7/22 16:38:06 网站建设 项目流程

首先分享一个好用的红薯数据爬取平台:GalaxyAPI

简介:小红书手机app分享的链接需要点击才能获取完成链接,本文教大家如何通过代码的方式将xhs的短连接转化为长链接。

1.正常我们分享的链接是这样的:

44 小猪吃宵夜发布了一篇小红书笔记,快来看吧! 😆 KeA1GIGiSMXGWy7 😆 http://xhslink.com/a/sT7omKb6ijX6,复制本条信息,打开【小红书】App查看精彩内容!

转换后是这样的:

https://www.xiaohongshu.com/discovery/item/677e4920000000000800d083?app_platform=ios&app_version=8.69.4&share_from_user_hidden=true&xsec_source=

app_share&type=normal&xsec_token=CBf6zZ8TKy3B6Gj5Xa21gIMrswg0W1nHWtTd

q1LTHCPxQ=&author_share=1&xhsshare=CopyLink&shareRedId=N0hINUhLPEA5N0

Y6TTgwNjY0Sz40PDZO&apptime=1740921786&share_id=d48e487a61b74e4e82bf95

a3e97c64cb

2.我们先写个代码定义提取这段文字的链接部分

def extract_url(text): # 正则表达式匹配小红书短链 pattern = r'http://xhslink\.com/\S+' match = re.search(pattern, text) if match: return match.group(0) # 返回匹配到的链接 else: return "未找到链接"

3. 接着我们将获取的链接通过重定向的方式获取到完整链接:

def get_redirect_url(short_url): try: # 添加请求头,模拟浏览器请求 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.9", "Connection": "keep-alive", } # 发送请求,禁止自动重定向 response = requests.get(short_url, headers=headers, allow_redirects=False) # 检查响应状态码 if response.status_code in [301, 302, 307]: # 处理重定向状态码 redirect_url = response.headers['Location'] # 获取重定向链接 return redirect_url else: return "无法获取重定向链接,状态码: {}".format(response.status_code) except Exception as e: return "请求失败: {}".format(str(e))

4.我们运行代码就会得到这样的链接:

完整链接: https://www.xiaohongshu.com/discovery/item/677e4920000000000800d083?app_platform=ios&app_version=8.69.4&share_from_user_hidden=true&xsec_source=

app_share&type=normal&xsec_token=CBf6zZ8TKy3B6Gj5Xa21gIMrswg0W1nHWtTd

q1LTHCPxQ=&author_share=1&xhsshare=CopyLink&shareRedId=N0hINUhLPEA5N0

Y6TTgwNjY0Sz40PDZO&apptime=1740921786&share_id=d48e487a61b74e4e82bf95

a3e97c64cb

完整代码:

import requests import re def extract_url(text): # 正则表达式匹配小红书短链 pattern = r'http://xhslink\.com/\S+' match = re.search(pattern, text) if match: return match.group(0) # 返回匹配到的链接 else: return "未找到链接" def get_redirect_url(short_url): try: # 添加请求头,模拟浏览器请求 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.9", "Connection": "keep-alive", } # 发送请求,禁止自动重定向 response = requests.get(short_url, headers=headers, allow_redirects=False) # 检查响应状态码 if response.status_code in [301, 302, 307]: # 处理重定向状态码 redirect_url = response.headers['Location'] # 获取重定向链接 return redirect_url else: return "无法获取重定向链接,状态码: {}".format(response.status_code) except Exception as e: return "请求失败: {}".format(str(e)) if __name__ == '__main__': text = '44 小猪吃宵夜发布了一篇小红书笔记,快来看吧! 😆 KeA1GIGiSMXGWy7 😆 http://xhslink.com/a/sT7omKb6ijX6,复制本条信息,打开【小红书】App查看精彩内容!' short_url = extract_url(text) full_url = get_redirect_url(short_url) print("完整链接:", full_url)

觉得有用的伙伴可以给个点赞关注哦!!!

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询