我有两个数字(NUM1,NUM2)文本行,我试图提取跨越具有相同格式的网页:提取使用BeautifulSoup
<div style="margin-left:0.5em;">
<div style="margin-bottom:0.5em;">
NUM1 and NUM2 are always followed by the same text across webpages
</div>
我想,正则表达式可能是要走的路对于这些特定的领域。下面是我尝试(从各种渠道借来的):
def nums(self):
nums_regex = re.compile(r'\d+ and \d+ are always followed by the same text across webpages')
nums_match = nums_regex.search(self)
nums_text = nums_match.group(0)
digits = [int(s) for s in re.findall(r'\d+', nums_text)]
return digits
就其本身而言,一个功能之外,该代码指定文本的实际源(例如,nums_regex.search(文本))时的作品。但是,我正在修改另一个人的代码,而我自己以前从来没有真正使用过类或函数。下面是他们的代码示例:
@property
def title(self):
tag = self.soup.find('span', class_='summary')
title = unicode(tag.string)
return title.strip()
正如你可能已经猜到了,我的代码是行不通的。我得到的错误:
nums_match = nums_regex.search(self)
TypeError: expected string or buffer
它看起来像我没有正确喂养原文,但我该如何解决它?
尝试'nums_regex.search(self.soup.text)' – yurib
[我已经听过这个之前......](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –