如何从lin获取完整的url

2024-04-23 18:23:30 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我转到http://www.example.com/dir1/dir2/index.html,有以下链接:

<a href="/about/">Link</a><a href="/about">Link</a><a href="/page.html/">Link</a><a href="/page.html">Link</a>

我的问题是,我不知道什么时候应该把哪些部分连接在一起,因为有些部分有斜杠,有些没有,我必须考虑到这一点

http://www.example.com/dir1/dir2/index.html/about/

应该是真的

http://www.example.com/dir1/dir2/about/。你知道吗

另外,我如何判断/about是一个目录而不是一个文件? 我将python3.5urllib.request模块一起使用。你知道吗


Tags: 目录comhttpindex链接examplehtmlwww
1条回答
网友
1楼 · 发布于 2024-04-23 18:23:30

你可以试试这个-

base_url = "http://www.example.com/dir1/dir2"
next = "/index.html"

然后,对于每个链接,提取href属性并将其分配给next。每个链接的最终url将是base_url + next。你知道吗

相关问题 更多 >