正则表达式 + beautifulsoup
regex + beautifulsoup
我已经隔离了从 BeautifulSoup 获得的一行 HTML 我想 运行 正则表达式,但我不断得到 AttributeError: 'NoneType' object has no attribute 'groups'
我阅读了另一个 Whosebug 问题 (using regex on beautiful soup tags),但我看不出我需要做什么来解决我的问题。
这是我的相关代码部分(已提供url):
使用 rob 的正确正则表达式更新仍然抛出 dat 属性错误:
soup = BeautifulSoup(urlopen(url).read()).find("div",{"id":"page"})
addy = soup.find("p","addy").em.encode_contents()
extracted_entities = re.match(r'$([\d.]+)\. ([^,]+), ([\d-]+)', addy)
extracted_entities.groups()
price = extracted_entities[0]
location = extracted_entities[1]
phone = extracted_entities[2]
addy好像是我想要的,返回:
. 2109 W. Chicago Ave., 773-772-0406, <a href="http://www.theoldoaktap.com/">theoldoaktap.com</a>
. 800 W. Randolph St., 312-929-4580, <a href="http://aucheval.tumblr.com/">aucheval.tumblr.com</a>
.50. 445 N. Clark St., 312-334-3688, <a href="http://www.rickbayless.com/">rickbayless.com</a>
等等,当我打印它时。
这是怎么回事?在此先感谢大家。
这个问题似乎是你的 RegEx 模式中的一个偏离 "
,我在你的示例输出中没有看到。
match = re.match(r'$([\d.]+)\. ([^,]+), ([\d-]+)', addy)
if match:
extracted_entities = match.groups()
else:
raise Exception("RegEx didn't match '%s'" % addy)
应该有效:
>>> f = """. 2109 W. Chicago Ave., 773-772-0406, <a href="http://www.theoldoaktap.com/">theoldoaktap.com</a>
... . 800 W. Randolph St., 312-929-4580, <a href="http://aucheval.tumblr.com/">aucheval.tumblr.com</a>
... .50. 445 N. Clark St., 312-334-3688, <a href="http://www.rickbayless.com/">rickbayless.com</a>"""
>>> l = f.splitlines()
>>> for i in l:
... r = re.match(r'$([\d.]+)\. ([^,]+), ([\d-]+)', i)
... if r:
... print "GOT IT", r.groups()
... else:
... print "NO GOT IT", i
...
GOT IT ('10', '2109 W. Chicago Ave.', '773-772-0406')
GOT IT ('9', '800 W. Randolph St.', '312-929-4580')
GOT IT ('9.50', '445 N. Clark St.', '312-334-3688')
我已经隔离了从 BeautifulSoup 获得的一行 HTML 我想 运行 正则表达式,但我不断得到 AttributeError: 'NoneType' object has no attribute 'groups'
我阅读了另一个 Whosebug 问题 (using regex on beautiful soup tags),但我看不出我需要做什么来解决我的问题。
这是我的相关代码部分(已提供url): 使用 rob 的正确正则表达式更新仍然抛出 dat 属性错误:
soup = BeautifulSoup(urlopen(url).read()).find("div",{"id":"page"})
addy = soup.find("p","addy").em.encode_contents()
extracted_entities = re.match(r'$([\d.]+)\. ([^,]+), ([\d-]+)', addy)
extracted_entities.groups()
price = extracted_entities[0]
location = extracted_entities[1]
phone = extracted_entities[2]
addy好像是我想要的,返回:
. 2109 W. Chicago Ave., 773-772-0406, <a href="http://www.theoldoaktap.com/">theoldoaktap.com</a>
. 800 W. Randolph St., 312-929-4580, <a href="http://aucheval.tumblr.com/">aucheval.tumblr.com</a>
.50. 445 N. Clark St., 312-334-3688, <a href="http://www.rickbayless.com/">rickbayless.com</a>
等等,当我打印它时。
这是怎么回事?在此先感谢大家。
这个问题似乎是你的 RegEx 模式中的一个偏离 "
,我在你的示例输出中没有看到。
match = re.match(r'$([\d.]+)\. ([^,]+), ([\d-]+)', addy)
if match:
extracted_entities = match.groups()
else:
raise Exception("RegEx didn't match '%s'" % addy)
应该有效:
>>> f = """. 2109 W. Chicago Ave., 773-772-0406, <a href="http://www.theoldoaktap.com/">theoldoaktap.com</a>
... . 800 W. Randolph St., 312-929-4580, <a href="http://aucheval.tumblr.com/">aucheval.tumblr.com</a>
... .50. 445 N. Clark St., 312-334-3688, <a href="http://www.rickbayless.com/">rickbayless.com</a>"""
>>> l = f.splitlines()
>>> for i in l:
... r = re.match(r'$([\d.]+)\. ([^,]+), ([\d-]+)', i)
... if r:
... print "GOT IT", r.groups()
... else:
... print "NO GOT IT", i
...
GOT IT ('10', '2109 W. Chicago Ave.', '773-772-0406')
GOT IT ('9', '800 W. Randolph St.', '312-929-4580')
GOT IT ('9.50', '445 N. Clark St.', '312-334-3688')