我有以下Python代码:
def getAddress(text):
text = re.sub('\t', '', text)
text = re.sub('\n', '', text)
blocks = re.findall('<div class="result-box" itemscope itemtype="http://schema.org/LocalBusiness">([a-zA-Z0-9 ",;:\.#&_=()\'<>/\\\t\n\-]*)</span>Follow company</span>', text)
name = ''
strasse = ''
locality = ''
plz = ''
region = ''
i = 0
for block in blocks:
names = re.findall('class="url">(.*)</a>', block)
strassen = re.findall('<span itemprop="streetAddress">([a-zA-Z0-9 ,;:\.&#]*)</span>', block)
localities = re.findall('<span itemprop="addressLocality">([a-zA-Z0-9 ,;:&]*)</span>', block)
plzs = re.findall('<span itemprop="postalCode">([0-9]*)</span>', block)
regions = re.findall('<span itemprop="addressRegion">([a-zA-Z]*)</span>', block)
try:
for name in names:
name = str(name)
name = re.sub('<[^<]+?>', '', name)
break
for strasse in strassen:
strasse = str(strasse)
strasse = re.sub('<[^<]+?>', '', strasse)
break
for locality in localities:
locality = str(locality)
locality = re.sub('<[^<]+?>', '', locality)
break
for plz in plzs:
plz = str(plz)
plz = re.sub('<[^<]+?>', '', plz)
break
for region in regions:
region = str(region)
region = re.sub('<[^<]+?>', '', region)
break
except:
continue
print i
i = i + 1
if plz == '':
plz = getZipCode(strasse, locality, region)
address = '"' + name + '"' + ';' + '"' + strasse + '";' + locality + ';' + str(plz) + ';' + region + '\n'
#saveToCSV(address)
我想过滤掉这个html代码段。这段剪辑重复几次。我希望函数为每个代码段返回一个条目。但相反,它返回一个包含两个片段的条目。我需要改变什么?
<div class="result-box" itemscope itemtype="http://schema.org/LocalBusiness">
<div class="clear">
<h2 itemprop="name"><a href="http://www.manta.com/c/mxlk5yt/belgium-jewelers-corp" class="url">Belgium Jewelers Corp</a></h2> </div>
<div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"> <span itemprop="addressLocality">Lawrenceville</span> <span itemprop="addressRegion">NJ</span>
</div> <a href="#" class="followCompany" data-emid="mxlk5yt" data-companyname="Belgium Jewelers Corp" data-location="ListingFollowButton" data-location-page="Megabrowse">
<span class="followMsg"><span class="followIcon mrs"></span>Follow company</span>
<span class="followingMsg"><span class="followIcon mrs"></span>Following</span>
<span class="unfollowMsg"><span class="followIcon mrs"></span>Unfollow company</span>
</a> <p class="type">Jewelry Stores</p> </div>
</li> <li> <div class="icons">
<ul> </ul>
</div>
答案 0 :(得分:4)
请放下那把锤子; HTML 不是一个正则表达形状的指甲。用于解析HTML的正则表达式变得非常复杂,并且非常脆弱,当HTML发生微妙变化时很容易被破坏。
改为使用正确的HTML解析器。 BeautifulSoup会使您的任务变得微不足道:
from bs4 import BeautifulSoup
soup = BeautifulSoup(text)
for block in soup.find_all('div', class_="result-box", itemtype="http://schema.org/LocalBusiness"):
print block.find('a', class_='url').string
street = block.find('span', itemprop="streetAddress")
if street:
print street.string
locality = block.find('span', itemprop="addressLocality")
if locality:
print locality.string
# .. etc. ..
答案 1 :(得分:0)
您应该查看HTMLParser
(documentation)的Python。正则表达式解析HTML非常糟糕。