如何使用 python-2.7 提及 url 的本地驱动器路径

How to mention the local drive path to the url using python-2.7

参考this link我可以找回路径和文件名

此外,我想将文件命名为-

D:\Links\filename

例如- http://www.example.com/category/location/filename - 将是原来的

D:\Links\category\location\filename.html - 是我想做的,因为它会 运行 为 -

fttp://D:/Links/category/location/filename.html

欢迎任何形式的帮助/指导。

非常感谢:)

您可以使用如下内容:

import urlparse
import os

url = "http://www.example.com/category/location/filename"

url_path = urlparse.urlparse(url).path
local_filename = os.path.join(r'd:', os.sep, 'links', os.path.normpath(url_path)[1:]) + ".html"
print local_filename

这将为您提供如下本地文件名:

d:\links\category\location\filename.html

Python urlparse function can be used to extract the path from your URL, and then the os.path.normpath function can be used to fix all of the separators. Finally, os.path.join 可用于将您的所有部件安全地重新组合在一起。