在Python的urlparse中,您可以使用urlparse来解析URL,然后使用parse_qsl来解析查询.
我想删除一个查询(名称,值)对,然后重新构建URL.
有一个urlunparse方法,但没有unparse_qsl方法.
从qsl列表重建查询的正确方法是什么?
这个简单的代码使人urlparse发疯,并且无法正确获取主机名,但将其设置为None:
from urllib.parse import urlparse
parsed = urlparse("google.com/foo?bar=8")
print(parsed.hostname)
Run Code Online (Sandbox Code Playgroud)
我想念什么吗?
Python的urlparse函数将url解析为六个组件(scheme,netloc,path和其他东西)
现在我发现解析"example.com/path/file.ext"不返回netloc,而是返回路径"example.com/path/file.ext".
不应该是netloc ="example.com"和path ="/ path/file.ext"吗?
我们真的需要一个"://"来确定是否存在netloc?
Python的门票:http://bugs.python.org/issue8284
我有一个Python App Engine Web应用程序类,我使用以下POST URL访问: http://localhost:8087/moderate?5649364211118945661=on
我怎样才能获得参数名称-而不是价值的的5649364211118945661参数,但包含所有参数名的列表on值.
例如,在以下网址中:
http://localhost:8087/moderate?5649364211118945661=on&23984729386481734=on&456287432349725=on&6753847523429875=off
Run Code Online (Sandbox Code Playgroud)
我该怎么提取这个:
['5649364211118945661', '23984729386481734', '456287432349725']
Run Code Online (Sandbox Code Playgroud)
非常感谢.
我有一个 df,在标有 url 的列中,对于不同的用户,它有数千个链接,如下所示:
https://www.google.com/something
https://mail.google.com/anohtersomething
https://calendar.google.com/somethingelse
https://www.amazon.com/yetanotherthing
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
import urlparse
df['domain'] = ''
df['protocol'] = ''
df['domain'] = ''
df['path'] = ''
df['query'] = ''
df['fragment'] = ''
unique_urls = df.url.unique()
l = len(unique_urls)
i=0
for url in unique_urls:
i+=1
print "\r%d / %d" %(i, l),
split = urlparse.urlsplit(url)
row_index = df.url == url
df.loc[row_index, 'protocol'] = split.scheme
df.loc[row_index, 'domain'] = split.netloc
df.loc[row_index, 'path'] = split.path
df.loc[row_index, 'query'] = split.query
df.loc[row_index, 'fragment'] = split.fragment
Run Code Online (Sandbox Code Playgroud)
该代码能够正确解析和拆分 url,但速度很慢,因为我正在迭代 df 的每一行。有没有更有效的方法来解析 URL?
我怎样才能轻松地从git URL中提取主机名 ssh://git@gitlab.org.net:3333/org/repo.git
u = urlparse(s)
Run Code Online (Sandbox Code Playgroud)
给我
ParseResult(scheme='ssh', netloc='git@gitlab.org.net:3333', path='/org/repo.git', params='', query='', fragment='')
Run Code Online (Sandbox Code Playgroud)
这意味着netloc最接近我想要的东西,这给我留下了令人失望的工作量.
我应该这样做
u.netloc.split('@')[1].split(':')[0]
Run Code Online (Sandbox Code Playgroud)
或者是否有一个更好地处理它的库?
我有两个系统:
第一个按预期工作:
>>> urlparse.urlparse('foo://bar/?blu=1')
ParseResult(scheme='foo', netloc='bar', path='/', params='', query='blu=1', fragment='')
# sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)
第二个是不同的:
>>> urlparse.urlparse('foo://bar/?blu=1')
ParseResult(scheme='foo', netloc='bar', path='/?blu=1', params='', query='', fragment='')
#sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
两者都使用Python 2.7.
我想更改 URL 的主机名。
>>> import urllib
>>> url = "https://foo.bar.com:9300/hello"
>>> parsed = urllib.parse.urlparse(url)
>>> parsed
ParseResult(scheme='https', netloc='foo.bar.com:9300', path='/hello', params='', query='', fragment='')
Run Code Online (Sandbox Code Playgroud)
因为parsed是namedtuple,所以scheme可以替换:
>>> parsed_replaced = parsed._replace(scheme='http')
>>> urllib.parse.urlunparse(parsed_replaced)
'http://foo.bar.com:9300/hello'
Run Code Online (Sandbox Code Playgroud)
该parsed对象还有一个主机名属性:
>>> parsed.hostname
'foo.bar.com'
Run Code Online (Sandbox Code Playgroud)
但它不是namedtuple中的字段之一,所以不能像scheme那样被替换。
有没有办法只替换 URL 中的主机名?
我目前正在开展一个涉及拆分网址的项目.我已经使用urlparse模块来分解url,所以现在我只使用路径段.
问题是,当我尝试split()基于分隔符"/"的字符串来分隔目录时,我最终在列表中找到空字符串.
例如,当我执行以下操作时:
import urlparse
url = "http://example/url/being/used/to/show/problem"
parsed = urlparse.urlparse(url)
path = parsed[2] #this is the path element
pathlist = path.split("/")
Run Code Online (Sandbox Code Playgroud)
我得到了清单:
['', 'url', 'being', 'used', 'to', 'show', 'problem']
Run Code Online (Sandbox Code Playgroud)
我不想要这些空字符串.我意识到我可以通过制作一个没有它们的新列表来删除它们,但这看起来很草率.有没有更好的方法来删除空字符串和斜杠?
urlparse ×10
python ×9
python-3.x ×2
url ×2
deno ×1
pandas ×1
parsing ×1
url-parsing ×1
urllib ×1
urlsplit ×1