Python 解析URI参数和关键字值对
我想解析文本文件中URI/L中的参数和关键字值。不带值的参数也应包括在内。Python很好,但我愿意接受使用其他工具(如Perl或一行程序)的建议,这些工具也可以做到这一点 示例来源:Python 解析URI参数和关键字值对,python,parsing,url,Python,Parsing,Url,我想解析文本文件中URI/L中的参数和关键字值。不带值的参数也应包括在内。Python很好,但我愿意接受使用其他工具(如Perl或一行程序)的建议,这些工具也可以做到这一点 示例来源: www.domain.com/folder/page.php?date=2012-11-20 www2.domain.edu/folder/folder/page.php?l=user&x=0&id=1&page=http%3A//domain.com/page.html&uniq
www.domain.com/folder/page.php?date=2012-11-20
www2.domain.edu/folder/folder/page.php?l=user&x=0&id=1&page=http%3A//domain.com/page.html&unique=123456&refer=http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname&text=
www.domain.edu/some/folder/image.php?l=adm&y=5&id=2&page=http%3A//support.domain.com/downloads/index.asp&unique=12345
blog.news.org/news/calendar.php?view=month&date=2011-12-10
示例输出:
date=2012-11-20
l=user
x=0
page=http%3A//domain.com/page.html&unique=123456
refer=http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname
test=
l=adm
y=5
id=2
page=http%3A//support.domain.com/downloads/index.asp
unique=12345
view=month
date=2011-12-10
你不需要潜入脆弱的正则表达式世界 是作业的工具(有助于转义特殊字符): 印刷品:
date=2012-11-20
l=user
x=0
id=1
page=http%3A//domain.com/page.html
unique=123456
refer=http%3A//domain2.net/results.aspx%3Fq%3Dbob%20test%201.21%20some%26file%3Dname
text=
l=adm
y=5
id=2
page=http%3A//support.domain.com/downloads/index.asp
unique=12345
view=month
date=2011-12-10
希望对您有所帮助。您可以使用正则表达式提取所有对
>>> url = 'www2.domain.edu/folder/folder/page.php?l=user&x=0&id=1&page=http%3A//domain.com/page.html&unique=123456&refer=http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname&text='
>>> import re
>>> url = 'www2.domain.edu/folder/folder/page.php?l=user&x=0&id=1&page=http%3A//domain.com/page.html&unique=123456&refer=http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname&text='
>>> p = re.compile('.*?&(.*?)=(.*?)(?=&|$)')
>>> m = p.findall(url)
>>> m
[('x', '0'), ('id', '1'), ('page', 'http%3A//domain.com/page.html'), ('unique', '123456'), ('refer', 'http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname'), ('text', '')]
您甚至可以使用dict理解将所有数据打包在一起
>>> dic = {k:v for k,v in m}
>>> dic
{'text': '', 'page': 'http%3A//domain.com/page.html', 'x': '0', 'unique': '123456', 'id': '1', 'refer': 'http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname'}
然后,如果您只想打印出来:
>>> for k,v in dic.iteritems():
print k,'-->',v
text -->
page --> http%3A//domain.com/page.html
x --> 0
unique --> 123456
id --> 1
refer --> http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname
我会使用这样的正则表达式(首先是代码,然后是解释): 第一行是动作发生的地方。正则表达式查找一个单词后面跟一个等号的所有匹配项,然后查找以
&
或新行字符结尾的字符串。返回的pairs
是一个元组列表,其中每个元组包含单词(关键字)和值。我没有捕获=
符号,而是在循环中打印它
解释正则表达式:
\w+
表示一个或多个单词字符。它周围的括号表示捕获它并作为结果返回该值
=
-必须跟在单词后面的等号
*?
-以非贪婪的方式设置零个或多个字符,即直到出现新行或由\n|&
指定的&
符号。(?:…
模式意味着不应捕获\n
或&
由于我们在正则表达式中捕获了两个内容-关键字和=
符号后的所有内容,因此将返回一个2元组列表
re.S
告诉正则表达式引擎允许匹配所有正则表达式代码-
-在搜索中也包括新行字符,即允许搜索跨多行(这不是默认行为).如果您使用parse_qsl
而不是dict,您将得到一个列表。@KirkStrauser谢谢,使用parse_qsl
时代码看起来会更好。这不会取消任何值的引用。但更重要的是,urlparse.parse_qs
可以为您完成所有这些操作。另一种解决方案,它可以更容易地工作并移植到其他语言但不使用神奇的url库…DOWNVOTE!我之所以选择DOWNVOTE,是因为在一般情况下,如果您希望输出值与最初输入时的值相同,它实际上无法正常工作。您的解决方案会忽略一个escape unescape循环,因此对于大多数用例,输出都会被视为不正确。对于几乎任何一代像这样非常有用的函数,您可以假设它已经在标准库中定义。在这种情况下,请在“urlparse”中查找一些不涉及编写自己的regexp的替代方法。同意。请注意:不是我。抱歉,删除了我的评论
>>> for k,v in dic.iteritems():
print k,'-->',v
text -->
page --> http%3A//domain.com/page.html
x --> 0
unique --> 123456
id --> 1
refer --> http%3A//domain2.net/results.aspx%3Fq%3Dbob+test+1.21+some%26file%3Dname
pairs = re.findall(r'(\w+)=(.*?)(?:\n|&)', s, re.S)
for k, v in pairs:
print('{0} = {1}'.format(k, v))