Python提取值beautifulsoup正则表达式

时间:2013-07-17 13:44:31

标签: python regex beautifulsoup

我有以下通过正则表达式和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

请帮忙!

2 个答案:

答案 0 :(得分: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))

代码考虑到您的某些网址不包含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']

答案 1 :(得分: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']