我需要通過使用urllib.parse.urljoin從html form動作引數中插入url路徑到我的url,但是urljoin在這個例子中不能很好地作業,例如
。
網址。http://example.com/login/
形式。action="/test"
在網頁瀏覽器中點擊提交按鈕后會像這樣 http://example.com/login/test 但是在urljoin中會像這樣 http://example.com/test
>>> from urllib.parse import urljoin
>>> urljoin('http://example.com/login/'/span>,'/test'/span>)
"http://example.com/test/"。
我的問題是關于從動作引數得到的url,例如。action="/test"在瀏覽器中http://example.com/login/test在urllib中http://example.com/test但是如果值等于action="/test/"在瀏覽器和urllib中http://example.com/test/,我需要像瀏覽器一樣當值不以/
任何建議?
謝謝
uj5u.com熱心網友回復:
從urljoin的第二個引數中移除/,得到了預期的結果,即:http://example.com/login/test
from urllib.parse import urljoin
base = 'http://example.com/login/'/span>
path = '/test'/span>
urljoin(base, path.strip('/')
注意:urljoin應該模仿瀏覽器的確切行為。所以如果瀏覽器指向http://example.com/login/test,那么urljoin的輸出也應該如此(反之亦然)。你確定你想要的預期結果是什么嗎?
這個問題可能會提供更多的見解 - Python: urljoin的混亂情況
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314853.html
標籤:
上一篇:用Pandas決議一個表格
下一篇:用C語言決議逗號分隔的字串
