HTML页面中有如下格式的元素:
<td class="cell1"><b>Dave Mason's Traffic Jam</b></td><td class="cell2">Scottish Rite
Auditorium</td><td class="cell3">$29-$45</td><td class="cell4">On sale now</td><td class="cell5"><a
href="http://www.ticketmaster.com/dave-masons-traffic-jam-collingswood-new-jersey-11-29-2014/event
/02004B48C416D202?artistid=1033927&majorcatid=10001&minorcatid=1&tm_link=venue_msg-
1_02004B48C416D202" target="_blank">TIX</a></td><td class="cell6">AA</td><td
class="cell7">Philadelphia</td>我想使用python从文本中分别提取"Dave的交通堵塞“部分、”苏格兰礼堂“部分等。使用这个正则表达式'.*‘将从第一个标记返回到下一个换行符之前的最后一个标记。如何更改表达式,使其只返回标记对之间的块?
编辑:@HenryKeiter & @Hakiko,那会很棒,但是这个任务需要我使用python。
发布于 2014-05-10 22:38:13
这里有一个提示,不是一个完整的解决方案:在您的情况下,您需要使用一个非贪婪的regexp。基本上,你需要用
.*?而不是
.*非贪婪意味着最小的模式将被匹配。默认情况下-它是最大的。
发布于 2014-05-10 22:53:42
使用美汤
from bs4 import BeautifulSoup
html = '''
<td class="cell1"><b>Dave Mason's Traffic Jam</b></td><td class="cell2">Scottish Rite
Auditorium</td><td class="cell3">$29-$45</td><td class="cell4">On sale now</td><td class="cell5"><a
href="http://www.ticketmaster.com/dave-masons-traffic-jam-collingswood-new-jersey-11-29-2014/event
/02004B48C416D202?artistid=1033927&majorcatid=10001&minorcatid=1&tm_link=venue_msg-
1_02004B48C416D202" target="_blank">TIX</a></td><td class="cell6">AA</td><td
class="cell7">Philadelphia</td>
'''.strip()
soup = BeautifulSoup(html)
tds = soup.find_all('td')
contentList = []
for td in tds:
contentList.append(td.get_text())
print contentList返回
[u"Dave Mason's Traffic Jam", u'Scottish Rite\nAuditorium', u'$29-$45', u'On sale now', u'TIX', u'AA', u'Philadelphia']https://stackoverflow.com/questions/23586998
复制相似问题