คุณสามารถรับสาเหตุที่คุณไม่สามารถดูการเปลี่ยนเส้นทางในประวัติคำขอได้จาก คำตอบนี้ใน SO:
คำขอไม่แสดงการเปลี่ยนเส้นทางเนื่องจากคุณไม่ได้ถูกเปลี่ยนเส้นทางจริงๆ ในแง่ HTTP Wikipedia ใช้กลอุบาย JavaScript บางอย่าง (อาจเป็นการแก้ไขประวัติ HTML5 และ pushState) เพื่อเปลี่ยนที่อยู่ที่แสดงในแถบที่อยู่ แต่แน่นอนว่าใช้ไม่ได้กับคำขอ
หากคุณตรวจสอบแหล่งที่มาของหน้า
(view-source:https://en.wikipedia.org/wiki/Ronald_Aylmer_Fisher
) URL ที่เปลี่ยนเส้นทางจะอยู่ที่นี่:
<link rel="canonical" href="https://en.wikipedia.org/wiki/Ronald_Fisher"/>
คุณสามารถขูดมันได้โดยใช้ regex หรือ BeautifulSoup แบบนี้:
import re
import requests
from bs4 import BeautifulSoup
r = requests.get('https://en.wikipedia.org/wiki/Ronald_Aylmer_Fisher')
# Using regex (not sure if this is the best regex approach)
href_regex = re.compile(r'<link rel="canonical" href="/th(.*)"/>')
redirected_url = href_regex.search(r.text).groups()[0]
# Using BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
redirected_url = soup.find('link', rel='canonical')['href']
print(redirected_url)
เอาท์พุท:
https://en.wikipedia.org/wiki/Ronald_Fisher
หรือเพื่อให้ได้ผลลัพธ์ที่ต้องการ คุณสามารถทำได้ดังนี้:
print(redirected_url.split('/')[-1])
เอาท์พุท:
โรนัลด์_ฟิชเชอร์
person
Keyur Potdar
schedule
09.02.2018