-1

我尝试从 url 中提取用户请求。我尝试搜索答案,但我只找到如何解析字符串。但是我有一个问题,我应该识别很多带有请求的 url,当我尝试获取带有属性的字符串时,带有文本的属性是不同的。我的意思是当我尝试

pat = re.compile(r"\?\w+=(.*)")
search = ['yandex.ru/search', 'youtube.com/results', 'google.com/search', 'google.ru/search', 'go.mail.ru/search', 'search.yahoo.com/search', 'market.yandex.ru/search', 'bing.com/search']
for i in urls:
    u = re.findall(pat, i)
    if any(ext in i for ext in search):
        if len(u) > 0:
            str = urllib.unquote(u[0])
            print str
            print {k: [s for s in v] for k, v in parse_qs(str).items()}

它看起来像

chromesearch&clid=2196598&text=королевы крика смотреть онлайн&lr=213&redircnt=1467230336.1
{'text': ['\xd0\xba\xd0\xbe\xd1\x80\xd0\xbe\xd0\xbb\xd0\xb5\xd0\xb2\xd1\x8b \xd0\xba\xd1\x80\xd0\xb8\xd0\xba\xd0\xb0 \xd1\x81\xd0\xbc\xd0\xbe\xd1\x82\xd1\x80\xd0\xb5\xd1\x82\xd1\x8c \xd0\xbe\xd0\xbd\xd0\xbb\xd0\xb0\xd0\xb9\xd0\xbd'], 'clid': ['2196598'], 'lr': ['213'], 'redircnt': ['1467230336.1']}
минималистичный+стиль&newwindow=1&biw=1280&bih=909&source=lnms&tbm=isch&sa=X&ved=0ahUKEwikhI2M_s3NAhXBBiwKHfbEBEEQ_AUIBigB#imgrc=Er7qLiHoEGPIGM:
{'bih': ['909'], 'newwindow': ['1'], 'source': ['lnms'], 'ved': ['0ahUKEwikhI2M_s3NAhXBBiwKHfbEBEEQ_AUIBigB#imgrc=Er7qLiHoEGPIGM:'], 'tbm': ['isch'], 'biw': ['1280'], 'sa': ['X']}
минималистичный+стиль&newwindow=1&biw=1280&bih=909&source=lnms&tbm=isch&sa=X&ved=0ahUKEwikhI2M_s3NAhXBBiwKHfbEBEEQ_AUIBigB#imgrc=Er7qLiHoEGPIGM:
{'bih': ['909'], 'newwindow': ['1'], 'source': ['lnms'], 'ved': ['0ahUKEwikhI2M_s3NAhXBBiwKHfbEBEEQ_AUIBigB#imgrc=Er7qLiHoEGPIGM:'], 'tbm': ['isch'], 'biw': ['1280'], 'sa': ['X']}
rjulf+ddjlbim+ytdthysq+gby+rjl+d+,fyrjvfn&ie=utf-8&oe=utf-8&gws_rd=cr&ei=ezZ0V-7iOoab6ASvlJe4Dg
{'ie': ['utf-8'], 'oe': ['utf-8'], 'gws_rd': ['cr'], 'ei': ['ezZ0V-7iOoab6ASvlJe4Dg']}
маскаи гейла&lr=10750&clid=1985551-210&win=213
{'win': ['213'], 'clid': ['1985551-210'], 'lr': ['10750']}
1&q=как+выбрать+смартфон
{'q': ['\xd0\xba\xd0\xb0\xd0\xba \xd0\xb2\xd1\x8b\xd0\xb1\xd1\x80\xd0\xb0\xd1\x82\xd1\x8c \xd1\x81\xd0\xbc\xd0\xb0\xd1\x80\xd1\x82\xd1\x84\xd0\xbe\xd0\xbd']}
Jade+Jantzen&ie=utf-8&oe=utf-8&gws_rd=cr&ei=FQB0V9WbIoahsAH5zZGACg
{'ie': ['utf-8'], 'oe': ['utf-8'], 'gws_rd': ['cr'], 'ei': ['FQB0V9WbIoahsAH5zZGACg']}

有什么方法可以只获取所有字符串的文本吗?

4

1 回答 1

0

您可以使用字典查找来访问文本以获取列表,然后访问列表的第一个元素:

d = {'text': ['\xd0\xba\xd0\xbe\xd1\x80\xd0\xbe\xd0\xbb\xd0\xb5\xd0\xb2\xd1\x8b \xd0\xba\xd1\x80\xd0\xb8\xd0\xba\xd0\xb0 \xd1\x81\xd0\xbc\xd0\xbe\xd1\x82\xd1\x80\xd0\xb5\xd1\x82\xd1\x8c \xd0\xbe\xd0\xbd\xd0\xbb\xd0\xb0\xd0\xb9\xd0\xbd'], 'clid': ['2196598'], 'lr': ['213'], 'redircnt': ['1467230336.1']}
text = d['text'][0]

>>> print text
королевы крика смотреть онлайн

或者你可以直接从parse_qs结果中得到它:

>>> print urlparse.parse_qs(s)['text'][0]
королевы крика смотреть онлайн

要将其应用于您的代码,使其适用于所有值:

print {k: v[0] for k, v in parse_qs(str).items()}

即取每个值列表的第一项。


如果您想打印字典并让字符串以正确的表示形式出现,即不是由repr生成,您可以使用该json模块将字典对象转储为字符串,然后打印它们:

import json

d = {'text': ['\xd0\xba\xd0\xbe\xd1\x80\xd0\xbe\xd0\xbb\xd0\xb5\xd0\xb2\xd1\x8b \xd0\xba\xd1\x80\xd0\xb8\xd0\xba\xd0\xb0 \xd1\x81\xd0\xbc\xd0\xbe\xd1\x82\xd1\x80\xd0\xb5\xd1\x82\xd1\x8c \xd0\xbe\xd0\xbd\xd0\xbb\xd0\xb0\xd0\xb9\xd0\xbd'], 'clid': ['2196598'], 'lr': ['213'], 'redircnt': ['1467230336.1']}
s = json.dumps(d, ensure_ascii=False)

>>> print s
{"text": ["королевы крика смотреть онлайн"], "clid": ["2196598"], "lr": ["213"], "redircnt": ["1467230336.1"]}
于 2016-09-22T11:49:08.313 回答