【问题标题】:I need to scrape data between the <Br> tags using bs4我需要使用 bs4 在 <Br> 标签之间抓取数据
【发布时间】:2021-10-01 09:58:39
【问题描述】:

您好,其实我是在尝试在 br 标记之间刮取东西。 以下面的例子为例:-

<blockquote>
               <font face="Garamond" size="5"><br>
                 <b>ICM Partners<br></b>
                 730 Fifth Avenue<br>
                 New York, NY 10019<br>
                 (212) 556-5600<br>
                 <br>
                 (<i>Gelfman Schneider</i>)<br>
                 <br>
                 <a href="http://www.icmtalent.com"target="_blank">http://www.icmtalent.com</a> 
                 <br> </font></blockquote>

ICM 合作伙伴
第五大道 730 号
纽约,纽约 10019
(212) 556-5600

(格夫曼施耐德)

http://www.icmtalent.com

其实我想拆分成公司名称、公司地址、公司联系电话和公司网址。

我知道我可以通过拆分函数 .split(&lt;br&gt;) 拆分它,然后按顺序放置,但有时 地址不仅仅是 1 或 2 行,甚至在很多情况下联系电话也不是给定。所以我不能准确地放置任何东西。 以这种情况为例:-

<blockquote>
            <font face="Garamond" size="5"><br>
            <b>The Agency</b><br>
            24 Pottery Lane<br>
            Holland Park<br>
            London W11 4LZ<br>
            <br>
            <a href="http://theagency.co.uk" target="_blank">http://theagency.co.uk</a><br>
            </font></blockquote>

机构
陶艺巷24号
荷兰公园
伦敦 W11 4LZ

http://theagency.co.uk

希望你能解决。 提前谢谢您。

【问题讨论】:

  • 请阅读How to Ask 并以minimal reproducible example 的形式向我们展示您的尝试。 PS。它是 scrape 而不是 scrap
  • 您刚刚发现了为什么web semantics 很重要。有太多的猜测,看起来这个问题太宽泛了。例如电话号码:捕捉它们的最简单方法是编写正则表达式。你知道怎么做吗?
  • 您能否分享一个您用于解析此 HTML 的代码示例?

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

提取此类数据很容易出错,需要在更大的数据集上进行测试。

一种可能的方法是:

  1. 使用.stripped_strings 拆分整个条目,为您提供可能行的列表
  2. 使用正则表达式尝试查找包含电话号码的行。 如果未找到,则将地址的结尾设置为除最后一行之外的所有内容。
  3. 通过假设第一个条目是公司名称来创建一个条目,接下来的条目是地址,直到address_end,如果找到的话,可以选择电话条目。
  4. 假设最后一个条目是网站。

例如:

from bs4 import BeautifulSoup
import re

re_tel = re.compile(r'[0-9() -]{5,}$')

html = """
    <blockquote>
        <font face="Garamond" size="5"><br>
        <b>ICM Partners<br></b>
        730 Fifth Avenue<br>
        New York, NY 10019<br>
        (212) 556-5600<br>
        <br>
        (<i>Gelfman Schneider</i>)<br>
        <br>
        <a href="http://www.icmtalent.com"target="_blank">http://www.icmtalent.com</a> 
        <br></font>
    </blockquote>
    
    <blockquote>
        <font face="Garamond" size="5"><br>
        <b>The Agency</b><br>
        24 Pottery Lane<br>
        Holland Park<br>
        London W11 4LZ<br>
        <br>
        <a href="http://theagency.co.uk" target="_blank">http://theagency.co.uk</a><br>
            </font>
    </blockquote>    
    """
    
soup = BeautifulSoup(html, "html.parser")

for blockquote in soup.find_all('blockquote'):
    fields = list(blockquote.stripped_strings)
    tel = ''
    address_end = -1

    for index, field in enumerate(fields):
        if re_tel.match(field):
            tel = field
            address_end = index
            break

    fields = [fields[0], ', '.join(fields[1:address_end]), tel, fields[-1]]
    print(fields)

对于您的两个示例,这将给出:

['ICM Partners', '730 Fifth Avenue, New York, NY 10019', '(212) 556-5600', 'http://www.icmtalent.com']
['The Agency', '24 Pottery Lane, Holland Park, London W11 4LZ', '', 'http://theagency.co.uk']

在更大的数据集上进行测试时,这无疑需要改进。

【讨论】:

    猜你喜欢
    • 2022-11-19
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多