0

我通过正则表达式和beautifulsoup获得了以下内容。我需要提取 UID 值,例如 5968723334。

[u'/home.html', u'browse_settings.html', u'browse.html?', u'test.html?uid=5415292833', u'test.html?uid=5968723334', u'test.html?uid=5968723334', u'test.html?uid=5453943714', u'test.html?uid=5453943714', u'test.html?uid=6740871094', u'test.html?uid=6740871094', u'test.html?uid=5991868792', u'test.html?uid=5991868792', u'test.html?uid=25072413', u'test.html?uid=25072413', u'test.html?uid=6739965683', u'test.html?uid=6739965683', u'test.html?uid=7272910004', u'test.html?uid=7272910004', u'test.html?uid=13179298', u'test.html?uid=13179298', u'test.html?uid=5392816266', u'test.html?uid=5392816266', u'test.html?uid=5992588819', u'test.html?uid=5992588819', u'test.html?uid=6727114420', u'test.html?uid=6727114420', u'test.html?uid=7263648884', u'test.html?uid=7263648884', u'test.html?uid=5447240210', u'test.html?uid=5447240210', u'test.html?uid=5460515002', u'test.html?uid=5460515002', u'test.html?uid=5400731231', u'test.html?uid=5400731231', u'browse.html?params=_F_18_24_GB_0___grid_1', u'/home.html?t=1374068507', u'/account_info.html', u'http://www.example.com/browse.html?params=_F_18_24_GB_0___grid_0', u'http://www.example.com/contact.html', u'/logout.html', u'#top', u'/terms_of_service.html', u'http://safety.example.com']

我已经设法像这样提取了一个“uid”,但是我想提取所有的 UID:

>>> m = re.search("uid=(\d*)", soup.contents[0])
>>> print m
<_sre.SRE_Match object at 0x211b210>
>>> print m.group(1)
5442562712

请帮忙!

4

2 回答 2

0

您可以遍历列表并将正则表达式应用于每个列表:

uid = re.compile(r"uid=(\d*)")
uids = [match.group(1) for match in filter(None, map(uid.search, list_of_urls))]

以上是精简版:

uid = re.compile(r"uid=(\d*)")
uids = []
for url in list_of_urls:
    match = uid.search(url)
    if match is not None:
         uids.append(match.group(1))

该代码考虑到您的某些 url 不包含 UID 号。

演示:

>>> import re
>>> list_of_urls = [u'/home.html', u'browse_settings.html', u'browse.html?', u'test.html?uid=5415292833', u'test.html?uid=5968723334', u'test.html?uid=5968723334', u'test.html?uid=5453943714', u'test.html?uid=5453943714', u'test.html?uid=6740871094', u'test.html?uid=6740871094', u'test.html?uid=5991868792', u'test.html?uid=5991868792', u'test.html?uid=25072413', u'test.html?uid=25072413', u'test.html?uid=6739965683', u'test.html?uid=6739965683', u'test.html?uid=7272910004', u'test.html?uid=7272910004', u'test.html?uid=13179298', u'test.html?uid=13179298', u'test.html?uid=5392816266', u'test.html?uid=5392816266', u'test.html?uid=5992588819', u'test.html?uid=5992588819', u'test.html?uid=6727114420', u'test.html?uid=6727114420', u'test.html?uid=7263648884', u'test.html?uid=7263648884', u'test.html?uid=5447240210', u'test.html?uid=5447240210', u'test.html?uid=5460515002', u'test.html?uid=5460515002', u'test.html?uid=5400731231', u'test.html?uid=5400731231', u'browse.html?params=_F_18_24_GB_0___grid_1', u'/home.html?t=1374068507', u'/account_info.html', u'http://www.example.com/browse.html?params=_F_18_24_GB_0___grid_0', u'http://www.example.com/contact.html', u'/logout.html', u'#top', u'/terms_of_service.html', u'http://safety.example.com']
>>> uid = re.compile(r"uid=(\d*)")
>>> [match.group(1) for match in filter(None, map(uid.search, list_of_urls))]
[u'5415292833', u'5968723334', u'5968723334', u'5453943714', u'5453943714', u'6740871094', u'6740871094', u'5991868792', u'5991868792', u'25072413', u'25072413', u'6739965683', u'6739965683', u'7272910004', u'7272910004', u'13179298', u'13179298', u'5392816266', u'5392816266', u'5992588819', u'5992588819', u'6727114420', u'6727114420', u'7263648884', u'7263648884', u'5447240210', u'5447240210', u'5460515002', u'5460515002', u'5400731231', u'5400731231']
于 2013-07-17T14:01:35.737 回答
0

你要findall

>>> contents = [u'/home.html', u'browse_settings.html', u'browse.html?', u'test.html?uid=5415292833', u'test.html?uid=5968723334', u'test.html?uid=5968723334', u'test.html?uid=5453943714', u'test.html?uid=5453943714', u'test.html?uid=6740871094', u'test.html?uid=6740871094', u'test.html?uid=5991868792', u'test.html?uid=5991868792', u'test.html?uid=25072413', u'test.html?uid=25072413', u'test.html?uid=6739965683', u'test.html?uid=6739965683', u'test.html?uid=7272910004', u'test.html?uid=7272910004', u'test.html?uid=13179298', u'test.html?uid=13179298', u'test.html?uid=5392816266', u'test.html?uid=5392816266', u'test.html?uid=5992588819', u'test.html?uid=5992588819', u'test.html?uid=6727114420', u'test.html?uid=6727114420', u'test.html?uid=7263648884', u'test.html?uid=7263648884', u'test.html?uid=5447240210', u'test.html?uid=5447240210', u'test.html?uid=5460515002', u'test.html?uid=5460515002', u'test.html?uid=5400731231', u'test.html?uid=5400731231', u'browse.html?params=_F_18_24_GB_0___grid_1', u'/home.html?t=1374068507', u'/account_info.html', u'http://www.example.com/browse.html?params=_F_18_24_GB_0___grid_0', u'http://www.example.com/contact.html', u'/logout.html', u'#top', u'/terms_of_service.html', u'http://safety.example.com']
>>> import re
>>> m = re.findall("uid=(\d*)", " ".join(contents))
>>> m
[u'5415292833', u'5968723334', u'5968723334', u'5453943714', u'5453943714', u'6740871094', u'6740871094', u'5991868792', u'5991868792', u'25072413', u'25072413', u'6739965683', u'6739965683', u'7272910004', u'7272910004', u'13179298', u'13179298', u'5392816266', u'5392816266', u'5992588819', u'5992588819', u'6727114420', u'6727114420', u'7263648884', u'7263648884', u'5447240210', u'5447240210', u'5460515002', u'5460515002', u'5400731231', u'5400731231']
于 2013-07-17T14:07:39.940 回答