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

小红书app复制链接转换为直接可访问链接,网页版链接,小红书短链转长链(最新版) 首先分享一个好用的红薯数据爬取平台GalaxyAPI简介小红书手机app分享的链接需要点击才能获取完成链接本文教大家如何通过代码的方式将xhs的短连接转化为长链接。1.正常我们分享的链接是这样的44 小猪吃宵夜发布了一篇小红书笔记快来看吧 KeA1GIGiSMXGWy7 http://xhslink.com/a/sT7omKb6ijX6复制本条信息打开【小红书】App查看精彩内容转换后是这样的https://www.xiaohongshu.com/discovery/item/677e4920000000000800d083?app_platformiosapp_version8.69.4share_from_user_hiddentruexsec_sourceapp_sharetypenormalxsec_tokenCBf6zZ8TKy3B6Gj5Xa21gIMrswg0W1nHWtTdq1LTHCPxQauthor_share1xhsshareCopyLinkshareRedIdN0hINUhLPEA5N0Y6TTgwNjY0Sz40PDZOapptime1740921786share_idd48e487a61b74e4e82bf95a3e97c64cb2.我们先写个代码定义提取这段文字的链接部分def extract_url(text): # 正则表达式匹配小红书短链 pattern rhttp://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/xhtmlxml,application/xml;q0.9,image/webp,image/apng,*/*;q0.8, Accept-Language: en-US,en;q0.9, Connection: keep-alive, } # 发送请求禁止自动重定向 response requests.get(short_url, headersheaders, allow_redirectsFalse) # 检查响应状态码 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_platformiosapp_version8.69.4share_from_user_hiddentruexsec_sourceapp_sharetypenormalxsec_tokenCBf6zZ8TKy3B6Gj5Xa21gIMrswg0W1nHWtTdq1LTHCPxQauthor_share1xhsshareCopyLinkshareRedIdN0hINUhLPEA5N0Y6TTgwNjY0Sz40PDZOapptime1740921786share_idd48e487a61b74e4e82bf95a3e97c64cb完整代码import requests import re def extract_url(text): # 正则表达式匹配小红书短链 pattern rhttp://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/xhtmlxml,application/xml;q0.9,image/webp,image/apng,*/*;q0.8, Accept-Language: en-US,en;q0.9, Connection: keep-alive, } # 发送请求禁止自动重定向 response requests.get(short_url, headersheaders, allow_redirectsFalse) # 检查响应状态码 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)觉得有用的伙伴可以给个点赞关注哦