如何查找包含格式文本的类'blue'
的所有span:
04/18/13 7:29pm
因此可以是:
04/18/13 7:29pm
或:
Posted on 04/18/13 7:29pm
就构建这样做的逻辑而言,这就是我目前所得到的:
new_content = original_content.find_all('span', {'class' : 'blue'}) # using beautiful soup's find_all
pattern = re.compile('<span class=\"blue\">[data in the format 04/18/13 7:29pm]</span>') # using re
for _ in new_content:
result = re.findall(pattern, _)
print result
我指的是https://stackoverflow.com/a/7732827和https://stackoverflow.com/a/12229134想办法做到这一点,但以上就是我目前为止所拥有的一切。
编辑:
为了澄清这种情况,span提供了:
<span class="blue">here is a lot of text that i don't need</span>
以及
<span class="blue">this is the span i need because it contains 04/18/13 7:29pm</span>
注意,我只需要04/18/13 7:29pm
而不需要其他内容。
编辑2:
我也试过:
pattern = re.compile('<span class="blue">.*?(\d\d/\d\d/\d\d \d\d?:\d\d\w\w)</span>')
for _ in new_content:
result = re.findall(pattern, _)
print result
并得到错误:
'TypeError: expected string or buffer'
输出:
这种模式似乎满足了您的需求:
这是一个灵活的regex,您可以使用:
示例:
相关问题 更多 >
编程相关推荐