【问题标题】:A little clue for some basic HTML parsing with BSoup 4?使用 BSoup 4 进行一些基本 HTML 解析的一点线索?
【发布时间】:2013-11-13 15:35:46
【问题描述】:

我在这方面完全是新手,但是两天来一直试图用 BeautifulSoup 解析一些 HTML,但没有任何真正的好结果。 有一次我设法删除所有 HTML 并保留我想要的文本,但在我正在解析的整个表上只得到 1 个结果,而另一次我得到了我想要的一切,但似乎无法全部删除HTML。

from bs4 import BeautifulSoup

soup = BeautifulSoup (open("PlusGrosCAVerif.htm"))


raisonsociale = soup.find('td', {'class' : 'verif_col1'})

for noms in raisonsociale:
    listenom = raisonsociale.get_text()
    print(listenom)

HTML 看起来像这样:

   <table id="verif_hitparade_donnees">
                    <tr id="verif_meslistes_thead">
                        <th class="verif_col1">Raison sociale</th>
                        <th class="verif_col2">CP</th>
                        <th class="verif_col3">Ville</th>
                                                    <th class="verif_col5">C.A.</th>
                    </tr>

                        <tr class="verif_result_tr_opaq2">
                            <td class="verif_col1"><a href="/societe/M-H-C-S-509553459/">M H C S</a></td>
                            <td class="verif_col2"><a href="/societe/M-H-C-S-509553459/">51200</a></td>
                            <td class="verif_col3"><a href="/societe/M-H-C-S-509553459/">EPERNAY</a></td>
                                                            <td class="verif_col5"><a href="/societe/M-H-C-S-509553459/">1 472 239 977&nbsp;&euro;</a></td>
                        </tr>

                        <tr class="verif_result_tr_opaq">
                            <td class="verif_col1"><a href="/societe/VIVESCIA-302715966/">VIVESCIA</a></td>
                            <td class="verif_col2"><a href="/societe/VIVESCIA-302715966/">51100</a></td>
                            <td class="verif_col3"><a href="/societe/VIVESCIA-302715966/">REIMS</a></td>
                                                            <td class="verif_col5"><a href="/societe/VIVESCIA-302715966/">1 277 349 946&nbsp;&euro;</a></td>
                        </tr>

                        <tr class="verif_result_tr_opaq2">
                            <td class="verif_col1"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">SOC COOP APPROVISIONNEMENT PARIS EST</a></td>
                            <td class="verif_col2"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">51520</a></td>
                            <td class="verif_col3"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">SAINT MARTIN SUR LE PRE</a></td>
                                                            <td class="verif_col5"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">1 249 176 407&nbsp;&euro;</a></td>
                        </tr>

                        <tr class="verif_result_tr_opaq">
                            <td class="verif_col1"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">ARCELORMITTAL DISTRI SOLUTIONS FRANCE</a></td>
                            <td class="verif_col2"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">51100</a></td>
                            <td class="verif_col3"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">REIMS</a></td>
                                                            <td class="verif_col5"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">586 085 818&nbsp;&euro;</a></td>
                        </tr>

                        <tr class="verif_result_tr_opaq2">
                            <td class="verif_col1"><a href="/societe/SEVEAL-757803689/">SEVEAL</a></td>
                            <td class="verif_col2"><a href="/societe/SEVEAL-757803689/">51100</a></td>
                            <td class="verif_col3"><a href="/societe/SEVEAL-757803689/">REIMS</a></td>
                                                            <td class="verif_col5"><a href="/societe/SEVEAL-757803689/">480 141 491&nbsp;&euro;</a></td>
                        </tr>

                        <tr class="verif_result_tr_opaq">
                            <td class="verif_col1"><a href="/societe/ACOLYANCE-381960491/">ACOLYANCE</a></td>
                            <td class="verif_col2"><a href="/societe/ACOLYANCE-381960491/">51100</a></td>
                            <td class="verif_col3"><a href="/societe/ACOLYANCE-381960491/">REIMS</a></td>
                                                            <td class="verif_col5"><a href="/societe/ACOLYANCE-381960491/">462 996 287&nbsp;&euro;</a></td>
                        </tr>

...持续了很长一段时间。

我想做的是解析 td 类“verif_col”1、2、3 和 5,所以我可以把它们放在一个 CSV 文件中,所以我首先尝试获取名称 (verif_col1),去掉它们的任何html 左右。使用上面的代码,我只得到名字(MHCS),然后脚本停止。

我已经尝试过 findAll,但我无法使用 get_text() 方法让它工作。我想过 findNext() 之类的,但没有结果。

对于一个迷失和无知的新手有什么想法吗?

非常感谢

【问题讨论】:

    标签: python html beautifulsoup


    【解决方案1】:

    find_all代替find

    raisonsociale = soup.find_all('td', {'class' : 'verif_col1'})
    

    要获得文本,请获取text 属性:

    for noms in raisonsociale:
        print noms.text
    

    C'est 吹捧!希望这会有所帮助!

    【讨论】:

    • 感谢您的回答 :) 我已经尝试使用 find_all,但是当我这样做时,我收到此错误:“AttributeError: 'ResultSet' object has no attribute 'get_text'”。我也尝试使用 content() 而不是 get_text(),但无法让它工作。
    【解决方案2】:

    您可以使用正则表达式选择每个类名的1235,并将每一个保存在一个数组中以在最后打印出来,如下所示:

    from bs4 import BeautifulSoup
    import sys 
    import re
    import itertools as it
    
    tds = []
    
    soup = BeautifulSoup(open(sys.argv[1], 'r'), 'html')
    
    for td in soup.find_all('td', attrs={'class': re.compile(r'verif_col\d')}):
        tds.append(td.string)
    
    for i in range(len(tds) // 4): 
        print(','.join(it.islice(tds, i*4, 4*(i+1))))
    

    你可以像这样运行它:

    python3 script.py xmlfile
    

    产生:

    M H C S,51200,EPERNAY,1 472 239 977 €
    VIVESCIA,51100,REIMS,1 277 349 946 €
    SOC COOP APPROVISIONNEMENT PARIS EST,51520,SAINT MARTIN SUR LE PRE,1 249 176 407 €
    ARCELORMITTAL DISTRI SOLUTIONS FRANCE,51100,REIMS,586 085 818 €
    SEVEAL,51100,REIMS,480 141 491 €
    ACOLYANCE,51100,REIMS,462 996 287 €
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-13
      • 2013-02-07
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多