小红书app复制链接转换为直接可访问链接,网页版链接,小红书短链转长链(最新版)

简介:小红书手机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)

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

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

才华是浅浅的耐心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值