在http/https中使用urlparse在python中添加www
我想在HTTP/https后添加www特定的URL。这里是我的代码:在http/https中使用urlparse在python中添加www
import urllib from urllib.parse import urlunsplit
def addhttps(url):
replaced=urllib.parse.urlsplit(url)
if replaced.scheme=='http' or replaced.scheme=='https' or replaced.scheme=='':
replaced = replaced._replace(scheme="https")
return replaced.geturl()
def addWWW(url):
replaced=urllib.parse.urlsplit(url)
if replaced.netloc=='':
replaced = replaced._replace(netloc="www")
return replaced.geturl()
s="example.com"
c=addhttps(s)
d=addWWW(c)
print(c)
print(d)
我得到我不想输出:
https:///example.com https://www/example.com
我期待输出是这样的:
https://example.com https://www.example.com
感谢提前:)
回答:
如何使用urlparse(在python2.7中测试)
个from urlparse import urlparse s="example.com"
o = urlparse(s)
print "https://www.{0}".format(o.path)
回报
https://www.example.com
以上是 在http/https中使用urlparse在python中添加www 的全部内容, 来源链接: utcz.com/qa/265010.html