[英]Parse URL with a regex in Python
I want to get the query name and values to be displayed from a URL. For example, url='http://host:port_num/file/path/file1.html?query1=value1&query2=value2'
我想从 URL 获取要显示的查询名称和值。例如,
url='http://host:port_num/file/path/file1.html?query1=value1&query2=value2'
From this, parse the query names and its values and to print it.由此,解析查询名称及其值并打印出来。
I agree that it's best not to use a regular expression and better to use urlparse
, but here is my regular expression.我同意最好不要使用正则表达式,最好使用
urlparse
,但这是我的正则表达式。
Classes like urlparse
were developed specifically to handle all URLs efficiently and are much more reliable than a regular expression is, so make use of them if you can.像
urlparse
这样的类是专门为有效处理所有 URL 而开发的,并且比正则表达式更可靠,所以如果可以的话请使用它们。
>>> x = 'http://www.example.com:8080/abcd/dir/file1.html?query1=value1&query2=value2'
>>> query_pattern='(query\d+)=(\w+)'
>>> # query_pattern='(\w+)=(\w+)' a more general pattern
>>> re.findall(query_pattern, x)
[('query1', 'value1'), ('query2', 'value2')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.