urljoin function 路徑問題
2019/12/29 下午 03:24
Python網路爬蟲討論版
葉碩涵
觀看數:2
回答數:1
收藏數:2
pycrawler
想問urljoin('絕對路徑','相對路徑')
我在網路上查到
urljoin("http://www.chachabei.com/abc/folder/currentpage.html", "/folder2/anotherpage.html")
答案會是
'http://www.chachabei.com/folder2/anotherpage.html'
想請問要怎麼知道相對路徑應該拼在絕對路徑的哪個位置??
我的理解是'http://www.chachabei.com/abc/folder2/anotherpage.html'
然而解答中/abc並不會存在
謝謝
回答列表
-
2019/12/30 下午 05:32張維元 (WeiYuan)贊同數:3不贊同數:0留言數:0
要看你後面這個參數有沒有 / 符號,有的話會接到最原始的網址,沒有的話就會只取代最後一段。例如:
(1) urljoin("http://www.chachabei.com/abc/folder/currentpage.html","/folder2/anotherpage.html")
=> "http://www.chachabei.com/" + "/folder2/anotherpage.html"
(2) urljoin("http://www.chachabei.com/abc/folder/currentpage.html","folder2/anotherpage.html")
=> "http://www.chachabei.com/abc/folder/" + "folder2/anotherpage.html"