【问题标题】:Issue with Tables and Beautiful Soup餐桌和美味汤的问题
【发布时间】:2013-11-28 00:13:19
【问题描述】:

我正在尝试嵌套在 tr 标记中的标记,但我用来查找正确值的标识符嵌套在 tr 标记内的另一个 td 中。

也就是说,我使用的是网站LoLKing

并尝试根据名称(例如阿狸)抓取它以获取统计信息。

HTML 是:

<tr>
            <td data-sorttype="string" data-sortval="Ahri" style="text-align: left;">
                <div style="display: table-cell;">
                <div class="champion-list-icon" style="background:url(//lkimg.zamimg.com/shared/riot/images/champions/103_32.png)">
                    <a style="display: inline-block; width: 28px; height: 28px;" href="/champions/ahri"></a>
                </div>
                </div>
                <div style="display: table-cell; vertical-align: middle; padding-top: 3px; padding-left: 5px;"><a href="/champions/ahri">Ahri</a></div>
            </td>
            <td style="text-align: center;"  data-sortval="975"><img src='//lkimg.zamimg.com/images/rp_logo.png' width='18' class='champion-price-icon'>975</td>
            <td style="text-align: center;" data-sortval="6300"><img src='//lkimg.zamimg.com/images/ip_logo.png' width='18' class='champion-price-icon'>6300</td>
            <td style="text-align: center;" data-sortval="10.98">10.98%</td>
            <td style="text-align: center;" data-sortval="48.44">48.44%</td>
            <td style="text-align: center;" data-sortval="18.85">18.85%</td>
            <td style="text-align: center;" data-sorttype="string" data-sortval="Middle Lane">Middle Lane</td>
            <td style="text-align: center;" data-sortval="1323849600">12/14/2011</td>
        </tr> 

我在提取统计信息时遇到问题,这些统计信息嵌套在 data-sortval 之外的 td 标记中。我想我想提取所有 tr 标签,但我不知道如何根据包含带有 data-sortval="Ahri" 的 td 标签的标签提取 tr 标签。那时,我想遍历 tr 标签 x 次,直到达到我想要的第一个统计数据,10.98

目前,我正在尝试使用 data-sortval Ahri 对 td 进行查找,但它不会返回 tr 的其余部分。

如果一个更大的标签,不要把所有这些都嵌套在里面,这可能很重要:

  <table class="clientsort champion-list" width="100%" cellspacing="0" cellpadding="0">
    <thead>
    <tr><th>Champion</th><th>RP Cost</th><th>IP Cost</th><th>Popularity</th><th>Win Rate</th><th>Ban Rate</th><th>Meta</th><th>Released</th></tr>     
    </thead>
    <tbody>

我为不够清晰深表歉意,我是这个抓取术语的新手,但我希望这有足够的意义。 现在,我也在做:

main = soup.find('table', {'class':'clientsort champion-list'})

只得到那个表

编辑:

我为变量输入了这个:

for champ in champs:
    a = str(champ)
    print type(a) is str
    td_name = soup.find('td',{"data-sortval":a})

它确认 a 是一个字符串。 但它会抛出这个错误:

  File "lolrec.py", line 82, in StatScrape
    tr = td_name.parent
AttributeError: 'NoneType' object has no attribute 'parent'

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    大声笑!

    出于商业目的,请在抓取前阅读服务条款。

    (1) 要抓取英雄列表,您可以这样做,其逻辑与您描述的类似。

    from bs4 import BeautifulSoup
    import urllib2
    html = urllib2.urlopen('http://www.lolking.net/champions/')
    soup = BeautifulSoup(html)
    # locate the cell that contains hero name: Ahri 
    hero_list = ["Blitzcrank", "Ahri", "Akali"]
    for hero in hero_list:
        td_name = soup.find('td', {"data-sortval":hero})
        tr = td_name.parent
        popularity = tr.find_all('td', recursive=False)[3].text
        print hero, popularity
    

    输出

    Blitzcrank 12.58%
    Ahri 10.98%
    Akali 7.52%
    

    输出

    10.98%
    

    (2) 刮掉所有英雄。

    from bs4 import BeautifulSoup
    import urllib2
    html = urllib2.urlopen('http://www.lolking.net/champions/')
    soup = BeautifulSoup(html)
    # find the table first
    table = soup.find('table', {"class":"clientsort champion-list"})
    # find the all the rows
    for row in table.find('tbody').find_all("tr", recursive=False):
        cols = row.find_all("td")
        hero = cols[0].text.strip()
        popularity = cols[3].text
        print hero, popularity
    

    输出:

    Aatrox 6.86%
    Ahri 10.98%
    Akali 7.52%
    Alistar 4.9%
    Amumu 8.75%
    ...
    

    【讨论】:

    • 非常感谢!这实际上是出于研究目的,因为我是我大学的学生研究员。如果可能的话,我希望免费发布它,但我一定会按照您的建议去做并阅读服务条款。
    • 不过,我有一个问题。我将如何设法更改 soup.find('td', {"data-sortval":"Ahri"}) 以使用变量代替“Ahri”,比如说字典的所有键?目前,我将键转换为字符串,然后尝试在 for 循环中传递它们,但似乎 find 不会采用可变标头
    • soup.find("td", {"data-sortval":variable})
    • 你看,这是我的直觉,所以我做了更改(请参阅编辑后的帖子)并引发错误(请参阅编辑后的帖子)
    • 无视,我想通了。网站上的字符串有标点符号,我的数据结构中的字符串没有。这提出了一个有趣的问题。感谢您的帮助!
    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2020-02-29
    • 2019-03-13
    • 2013-09-10
    • 2011-08-07
    相关资源
    最近更新 更多