例如,我想加入资源路径的前缀路径,如/js/foo.js.
我希望生成的路径相对于服务器的根目录.在上面的例子中,如果前缀是"media",我希望结果是/media/js/foo.js.
os.path.join做得非常好,但它如何加入路径依赖于操作系统.在这种情况下,我知道我的目标是网络,而不是本地文件系统.
当您使用您知道将在URL中使用的路径时,是否有最佳选择?os.path.join会运行得好吗?我应该自己滚吗?
我正在尝试从HTML代码中获取所有href,并将其存储在列表中以供将来处理,例如:
示例网址:www.example-page-xl.com
<body>
<section>
<a href="/helloworld/index.php"> Hello World </a>
</section>
</body>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码列出href的:
import bs4 as bs4
import urllib.request
sauce = urllib.request.urlopen('https:www.example-page-xl.com').read()
soup = bs.BeautifulSoup(sauce,'lxml')
section = soup.section
for url in section.find_all('a'):
print(url.get('href'))
Run Code Online (Sandbox Code Playgroud)
但是我想将URL存储为:www.example-page-xl.com/helloworld/index.php而不仅仅是/helloworld/index.php的相对路径
不需要使用相对路径追加/加入URL,因为当我加入URL和相对路径时,动态链接可能会有所不同.
简而言之,我想刮掉绝对URL而不是单独的相对路径(并且没有加入)