python - 使用 beautifulsoup 访问未标记的文本

标签 python beautifulsoup

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

考虑以下一家美国公司的 html 数据:(已经是 soup 对象)

<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 代码提取邮政编码 (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 个帐户位于非美国。我只举了两个例子,我知道当前的方法并不是防弹的。在其他地址格式中,该脚本可能无法按预期工作,但我相信找出英国和德国的帐户将有很大帮助。

提前致谢

最佳答案

因为<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

关于python - 使用 beautifulsoup 访问未标记的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40314447/

相关文章:

python - Gspread 将电子表格导出到带格式的文件系统

Python 尝试从 youtube api 获取数据时出错

python-3.x - 如何从网络上抓取某一类属性的所有子级?

python - 使用 python 抓取具有多个部分的页面

python - 使用带有 UTF-8 的 soup.get_text()

Python - 循环 HTML 标签并使用 IF

android - 适用于 Android 的 PyQt5 GUI 与 Kivy GUI

python - csr_matrix.sort_indices 有什么作用?

python - 使用python将一个字符串拆分成它的句子

python - Beautifulsoup 部分提取字符串