使用beautifulsoup访问未标记的文本

时间:2016-10-28 23:16:46

标签: python beautifulsoup bs4

我正在使用python和beautifulsoup4来提取一些地址信息。 更具体地说,我在检索非美国邮政编码时需要帮助。

考虑以下美国公司的html数据:(已经是汤品)

<div class="compContent curvedBottom" id="companyDescription">
<div class="vcard clearfix">
<p id="adr">
<span class="street-address">999 State St Ste 100</span><br/>
<span class="locality">Salt Lake City,</span>
<span class="region">UT</span>
<span class="zip">84114-0002,</span>
<br/><span class="country-name">United States</span>
</p>
<p>
<span class="tel">
<strong class="type">Phone: </strong>+1-000-000-000
                            </span><br/>
</p>
<p class="companyURL"><a class="url ext" href="http://www.website.com" target="_blank">http://www.website.com</a></p>
</div>

</ul>
</div>

我可以使用以下python代码提取zipcode(84114-0002):

class CompanyDescription:
    def __init__(self, page):
        self.data = page.find('div', attrs={'id': 'companyDescription'})


    def address(self):
        #TODO: Also retrieve the Zipcode for UK and German based addresses - tricky!
        address = {'street-address': '', 'locality': '', 'region': '', 'zip': '', 'country-name': ''}
        for key in address:
            try:
                adr = self.data.find('p', attrs={'id': 'adr'})
                if adr.find('span', attrs={'class': key}) is None:
                    address[key] = ''
                else:
                    address[key] = adr.find('span', attrs={'class': key}).text.split(',')[0]

                # Attempting to grab another zip code value
                if address['zip'] == '':
                    pass

            except:
                # We should return a dictionary with "" as key adr
                return address

        return address

您可以看到我需要使用行if address['zip'] == '':

的一些建议

这两个汤对象的例子给我带来了麻烦。在下面我想检索 EC4N 4SA

<div class="compContent curvedBottom" id="companyDescription">
<div class="vcard clearfix">
<p id="adr">
<span class="street-address">Albert Buildings</span><br/>
<span class="extended-address">00 Queen Victoria Street</span>
<span class="locality">London</span>
                                    EC4N 4SA
                                    <span class="region">London</span>
<br/><span class="country-name">England</span>
</p>
<p>
</p>
<p class="companyURL"><a class="url ext" href="http://www.website.com.com" target="_blank">http://www.website.com.com</a></p>
</div>
<p><strong>Line of Business</strong> <br/>Management services, nsk</p> 
</div>

以及以下,我有兴趣获得 71364

<div class="compContent curvedBottom" id="companyDescription">
<div class="vcard clearfix">
<p id="adr">
<span class="street-address">Alfred-Kärcher-Str. 100</span><br/>
                                                71364
                                    <span class="locality">Winnenden</span>
<span class="region">Baden-Württemberg</span>
<br/><span class="country-name">Germany</span>
</p>
<p>
<span class="tel">
<strong class="type">Phone: </strong>+00-1234567
                            </span><br/>
<span class="tel"><strong class="type">Fax: </strong>+00-1234567</span>
</p>
</div>
</div>

现在,我在大约68,000个帐户上运行此程序,其中28,000个是非美国帐户。我只提出了两个例子,我知道目前的方法不是防弹。可能有其他地址格式,这个脚本没有按预期工作,但我相信找出英国和德国的帐户将有很大帮助。

提前致谢

1 个答案:

答案 0 :(得分:2)

因为只有<p>内没有标记的文字,所以你可以使用

find_all(text=True, recursive=False) 

仅获取文本(不含标签),但不获取嵌套标签(<span>)。这会为列表提供您的文字以及一些\n和空格,以便您可以使用join()创建一个字符串,并使用strip()删除所有\n和空格。

data = '''<p id="adr">
<span class="street-address">Albert Buildings</span><br/>
<span class="extended-address">00 Queen Victoria Street</span>
<span class="locality">London</span>
                                    EC4N 4SA
                                    <span class="region">London</span>
<br/><span class="country-name">England</span>
</p>'''

from bs4 import BeautifulSoup as BS

soup = BS(data, 'html.parser').find('p')

print(''.join(soup.find_all(text=True, recursive=False)).strip())

结果:EC4N 4SA

与第二个HTML相同

data = '''<p id="adr">
<span class="street-address">Alfred-Kärcher-Str. 100</span><br/>
                                                71364
                                    <span class="locality">Winnenden</span>
<span class="region">Baden-Württemberg</span>
<br/><span class="country-name">Germany</span>
</p>'''

from bs4 import BeautifulSoup as BS

soup = BS(data, 'html.parser').find('p')

print(''.join(soup.find_all(text=True, recursive=False)).strip())

结果:71364