简体   繁体   中英

Beautiful Soup Get list elements not separating by commas

I am trying to parse a list of cities from a website using Beautiful Soup

Here is the output: MonroeMatthewsWaxhawIndian Trail, Matthews

What I need is: Monroe, Matthews, Waxhaw, Indian Trail, Matthews

Here is the HTML:

<div id="current_tab">
    <p class="view_label_type_geoserved" id="view_label_field_geoserved">Geographies Served</p>
    <ul>
        <li class="view_type_geoserved" id="view_field_geoserved">
            <p style="font-weight: bold; border-bottom: 1px dotted #CCC; font-size: .9em;">North Carolina (NC)<span style="float: right; font-size: 0.8em;">North Carolina (NC)</span></p>
            <p style="margin: 5px 0 3px 8px; border-bottom: 1px dotted #DDD; font-size:1em">Union<span style="float: right; font-size: 0.8em;">Union</span></p>
            <div class="view_type_geoserved">
                <table>
                    <tr>
                        <td style="width: 225px;">Monroe</td>
                        <td>Matthews</td></tr><tr>
                        <td style="width: 225px;">Waxhaw</td>
                        <td>Indian Trail</td>
                    </tr>
                </table>
            </div>
            </li>
                <p style="margin: 5px 0 3px 8px; border-bottom: 1px dotted #DDD; font-size:1em">Mecklenburg<span style="float: right; font-size: 0.8em;">Mecklenburg</span></p>
                <div class="view_type_geoserved">
                    <table>
                        <tr>
                            <td style="width: 225px;">Matthews</td><td></td>
                        </tr>
                    </table>
                </div>
            </li>
    </ul>
</div>

and here is the code I am using to get the out I am getting.

geoserved_muni = ', '.join(p.text for p in soup.findAll("div", {"class":"view_type_geoserved"}))
print geoserved_muni

What am I missing to get those commas bewtwwen the individual tags?

Join every td element's text inside the div :

', '.join(td.get_text(strip=True) for td in soup.select("div.view_type_geoserved > table tr > td") if td.text) 

The expression inside soup.select() is a CSS selector .

Tested, for me it produces:

Monroe, Matthews, Waxhaw, Indian Trail, Matthews

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM