0

我试图解析以下内容HTML

<div class="content">
    <h3>
                                Kontaktuppgifter</h3>
    <table>
        <tr>
            <th>
                Postadress:
            </th>
            <td>
                Platteb....
                <br/>44497 SVE....
            </td>
        </tr>
        <tr>
            <th>
                Telefon:
            </th>
            <td>
                01-.......

            </td>
        </tr>
    </table>

我想抓住td 1td 2td 3 并不td 3总是存在。

这是我到目前为止得到的:

def ParsePage(threadName, page_url):
    r  = requests.get(page_url)
    print "\n--------------------\n"
    print "Parsing page: " + r.url
    data = r.text
    soup = BeautifulSoup(data)

    divs = soup.findAll('div', { "class" : "content" })
    for tag in divs:
        divds = tag.findAll('td')   
        print divds

出于某种原因,这只是打印整个 div

4

1 回答 1

1

你一定有错字,代码对我有用:

from bs4 import BeautifulSoup

soup = BeautifulSoup(your_html)
div = soup.findAll("div", {"class": "content"})

for tag in div: print tag.findAll("td")
#printed:
[<td>
                Platteb....
                <br/>44497 SVE....
            </td>, <td>
                01-.......

            </td>]
于 2013-11-07T15:39:54.347 回答