簡介:小紅書手機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_urlelse: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 redef 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_urlelse: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)
覺得有用的伙伴可以給個點贊關注哦!!!
?