1

使用 webbrowser 模块,我想在 last.fm 上打开一个特定页面。它从文本文件中选择一行然后打印出来。我希望它在末尾添加该行:

webbrowser.open('http://www.last.fm/music/')

例如,random.choice 选择示例艺术家。我希望将示例艺术家正确添加到 URL 的末尾。任何帮助表示赞赏。

4

1 回答 1

2

使用该urlparse.urljoin函数构建完整的目标 URL:

import urlparse
import webbrowser

artist_name = 'virt' 
url = urlparse.urljoin('http://www.last.fm/music/', artist_name)

# Will open http://www.last.fm/music/virt in your browser.
webbrowser.open(url)
于 2013-11-29T01:52:10.623 回答