在python中解析url以提取參數 (Parsing url in python to extract parameters)


問題描述

在python中解析url以提取參數 (Parsing url in python to extract parameters)

我在 python 中有一個 URL,如

localhost:8080&id=123&id=345&name=john

是否有一個庫可以解析這個並獲取所有參數的鍵值對?因為我想再次使用說 id feild 將 url 創建為初始參數的組合,所以我將有兩個新的 URL。

localhost:8080&id=123&name=johnlocalhost:8080&id=345&name=john

我在 python 2.7 中使用此代碼,但它並沒有帶來兩個 id 的

parse_url = urlparse(url)
query_dict = dict(parse_qsl(parse_url.query))
print query_dict

參考解法

方法 1:

I think you could accomplish this with a regex

import re
components = [re.split(":|=", component) for component in <url>.split("&")]
url_dict = {component[0]: component[1] for component in components}

(by John Constantinemattyx17)

參考文件

  1. Parsing url in python to extract parameters (CC BY‑SA 2.5/3.0/4.0)

#url-rewriting #Python #urllib






相關問題

靜態內容域的 IIS 重定向規則 (IIS redirect rules for static content domains)

IIS 重寫模塊重寫映射性能 (IIS Rewrite Module rewrite map performance)

我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)

如何將查詢參數修改為路徑文件夾名稱? (How to mod_rewrite a query parameter to a path folder name?)

如何使用 .htaccess 將動態 URL 更改為 SEO 友好 (how to change the dynamic URL to SEO friendly using .htaccess)

AngularJS 和 URL 重寫為 HTTPS (AngularJS and URL Rewrite to HTTPS)

mod-rewrite 結合了兩種不同的重寫規則 (mod-rewrite combine two different rewrite rules)

htaccess 刪除 .php 擴展名並重寫為 www (htaccess remove .php extension and rewrite to www)

初學者 Apache URL 重寫問題 (Beginner Apache URL Rewrite Question)

.net datapager + listview + Intelligencia.UrlRewriter (.net datapager + listview + Intelligencia.UrlRewriter)

jquery get / post請求的Apache url替換 (Apache url replacement for jquery get / post request)

在python中解析url以提取參數 (Parsing url in python to extract parameters)







留言討論