用Python属性获取字符串并链接到字典

2024-06-16 11:43:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在得到如下输出:

http://www.site.com/prof.php?pID=478http://www.site.com/prof.php?pID=693

在使用以下评论员的建议后,我有:

urls = [el.url for el in domainLinkOutput]
return HttpResponse(urls)

如何将此输出转换为python字典,如:

urls = { '0': 'http://www.site.com/prof.php?pID=478', '1': 'http://www.site.com/prof.php?pID=693' }

Tags: incomhttpurlforwwwsitepid
3条回答

我认为这里不需要regex-只需对Link对象使用属性访问。。。你知道吗

如果您有一个Link对象列表,那么可以使用如下命令:

urls = [el.url for el in list_of_objects]

你应该可以通过Link_object.url获得url。。。你知道吗

使用此正则表达式匹配URL:

url='([^']+)'

样本输出:

    [0] => http://www.somesite.com/prof.php?pID=478
    [1] => http://www.somesite.com/prof.php?pID=527
    [2] => http://www.somesite.com/prof.php?pID=645

如果要排除参数,请使用

url='([^'?]+)

样本输出:

    [0] => http://www.somesite.com/prof.php
    [1] => http://www.somesite.com/prof.php
    [2] => http://www.somesite.com/prof.php

你可以试试re.finditer。你知道吗

r = re.compile("url='(.*?)'")
for match in r.finditer(input):
    print match.group[1]

您可以阅读Python文档here。你知道吗

相关问题 更多 >