无法在BeautifulSoup中链接find和find_all

问题描述 投票:4回答:1

我有一本关于BeautifulSoup的书和文档。两者都说我应该能够链接find / find_all方法并使用下标从单页抓取中获得我想要的内容。情况似乎并非如此。请考虑下表。

<tr>
<td><span style="display:none;" class="sortkey">Dresser !</span><span class="sorttext">**<a href="/wiki/Louise_Dresser" title="Louise Dresser">Louise Dresser</a>**</span></td>
<td><span style="display:none;" class="sortkey">Ship !</span><span class="sorttext"><i><a href="/wiki/A_Ship_Comes_In" title="A Ship Comes In">A Ship Comes In</a></i></span></td>
<td><span style="display:none;" class="sortkey">Pleznik !</span><span class="sorttext">Mrs. Pleznik</span></td>
</tr>
<tr>
<td><span style="display:none;" class="sortkey">Swanson !</span><span class="sorttext"><a href="/wiki/Gloria_Swanson" title="Gloria Swanson">Gloria Swanson</a></span></td>
<td><i><a href="/wiki/Sadie_Thompson" title="Sadie Thompson">Sadie Thompson</a></i></td>
<td><span style="display:none;" class="sortkey">Thompson !</span><span class="sorttext">Sadie Thompson</span></td>
</tr>
<tr>
<th scope="row" rowspan="6" style="text-align:center"><a href="/wiki/1928_in_film" title="1928 in film">1928</a>/<a href="/wiki/1929_in_film" title="1929 in film">29</a><br />
<small><a href="/wiki/2nd_Academy_Awards" title="2nd Academy Awards">(2nd)</a></small></th>
<td style="background:#FAEB86"><b><span style="display:none;" class="sortkey">Pickford !</span><span class="sorttext">**<a href="/wiki/Mary_Pickford" title="Mary Pickford">Mary Pickford</a>**</span> <img alt="Award winner" src="//upload.wikimedia.org/wikipedia/commons/f/f9/Double-dagger-14-plain.png" width="9" height="14" data-file-width="9" data-file-height="14" /></b></td>

对于每个表行,我需要获取第一个元素,然后是第一个嵌套标记内的文本。 Lousie Dresser将成为第一个数据点,其次是Gloria Swanson,然后是Mary Pickford。

我以为以下会让我在那里,但我错了,6个小时后我就花了。

def getActresses(URL):
    try:
        html = urlopen(URL)
    except HTTPError:
        print("Page not found.")
        return None
    try:
        bsObj = BeautifulSoup(html, "lxml")
        soup = bsObj.find("table", {"class":"wikitable sortable"})
    except AttributeError:
        print("Error creating/navigating soup object")
    data = soup.find_all("tr").find_all("td").find("a").get_text()
    print(data)


getActresses("https://en.wikipedia.org/wiki/Academy_Award_for_Best_Actress")

这不是我尝试过的唯一代码。我尝试循环遍历行,然后表格数据单元格,然后访问标签。我已经尝试过要求标签,然后重新制作它们,只是被告知我没有我想要的文本。我尝试连锁操作时遇到的最常见的错误(如上所述)是AttributeError: 'ResultSet' object has no attribute 'find'.订阅绝对不起作用,即使复制书籍示例(去图?!)。此外,我已经让流程中止了,我不知道这是可能的。

关于正在发生什么以及为什么应该如此简单的事情的想法似乎是这样一个事件将受到极大的赞赏。

python web-scraping beautifulsoup
1个回答
7
投票
import requests
from bs4 import BeautifulSoup

def getActresses(URL):
    res = requests.get(URL)

    try:
        soup = BeautifulSoup(res.content, "lxml")
        table = soup.find("table", {"class":"wikitable sortable"})
    except AttributeError:
        print("Error creating/navigating soup object")

    tr = table.find_all("tr")

    for _tr in tr:
        td = _tr.find_all("td")
        for _td in td:
            a = _td.find_all("a")
            for _a in a:
                print(_a.text.encode("utf-8"))

getActresses("https://en.wikipedia.org/wiki/Academy_Award_for_Best_Actress")

使用text而不是get_text()抱歉我使用requests模块来演示

find_all方法总是返回一个列表,所以你必须循环它

© www.soinside.com 2019 - 2024. All rights reserved.