【问题标题】:Strong instances under Divs - only capturing initial instanceDivs 下的强实例 - 仅捕获初始实例
【发布时间】:2019-09-21 08:34:14
【问题描述】:

我的代码有一个小问题。实际上,我的目标是从一些代码中获取以下实例:

<td>
<div>
<div>8 of 15 — <strong>53%</strong></div>
<div><div style="width: 100%"><div style="width: 53%"></div></div>
</div>
</div>
</td>

我试图在 div 中捕获强数据之前的数据,以及强百分比。目前正在使用以下内容获取这些数据,但我只能获取 a) 第一个实例或 b) 空值:

from bs4 import BeautifulSoup as bsoup
import requests as reqs

other_stat_list = []

page_to_parse = 'https://fbref.com/en/matches/033092ef/Northampton-Town-Lincoln-City-August-4-2018-League-Two'

page = reqs.get(page_to_parse)
status_code = page.status_code
status_code = str(status_code)
parse_page = bsoup(page.content, 'html.parser')


find_other_stats = parse_page.find_all('div', id="team_stats")
for stat in find_other_stats:
    add_other_stats = stat.find_next('strong').get_text()
    other_stat_list.append(add_other_stats)
    print(add_other_stats)

我之前也遇到过类似的问题,不是所有的 div 实例都被捕获。我运行了一个递归来捕获代码中的所有孩子:

find_other_stats = parse_page.find_all('div', id="team_stats")
all_other_stats = find_other_stats[0].find_all('div', recursive=False)
for stat in all_other_stats:
    add_other_stats = find_next('strong').get_text()
     other_stat_list.append(add_team)

但是,它的一个变体也会产生空值,所以不确定为什么递归不起作用。

期望得到“53%”,理想情况下是 '8 of 15 -。我可以自己解析这些值,但捕获它们看起来比我预期的要难。感谢您的帮助!

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    使用此代码,我将所有部分作为单独的元素。

    第一:只有一个divid="team_stats" 所以我使用find() 而不是find_all()

    我搜索 td 而不是搜索 div 并使用 get_text() 我在单元格中获得全文。这样我就不需要对嵌套的&lt;div&gt; 进行递归,也不会从空的&lt;div&gt; 中得到空字符串。

    从表格中的所有单元格获取所有文本后,我将其拆分为更小的部分并清理它们。

    我将它除以 以得到 53%8 of 15 作为单独的元素。但是 不正常- 所以我手动将它从 HTML 复制/粘贴到代码中。

    我发现还有代码为'\xa0' 的字符(可以是“不间断空间”或类似的东西),我使用strip('\xa0') 清理它 - 我也可以使用rstrip()/lstrip()replace()`` or slice it with[1:]and[:-1]`.

    from bs4 import BeautifulSoup as BS
    import requests 
    
    url = 'https://fbref.com/en/matches/033092ef/Northampton-Town-Lincoln-City-August-4-2018-League-Two'
    
    response = requests.get(url)
    soup = BS(response.content, 'html.parser')
    
    # --- getting data ---
    
    data = []
    
    stats = soup.find('div', id="team_stats")
    
    for row in stats.find_all('td'):
        text = row.get_text(strip=True)
        data.append(text)
    
    # --- splitting and cleaning data ---
    
    print('Possession:', data[0], '|', data[1])
    
    text1, percent1 = data[2].split('—')
    percent2, text2 = data[3].split('—')
    text1 = text1.strip('\xa0')
    text2 = text2.strip('\xa0')
    print('Shots on Target:', text1, '|', percent1, '|', text2, '|', percent2)
    
    text1, percent1 = data[4].split('—')
    percent2, text2 = data[5].split('—')
    text1 = text1.strip('\xa0')
    text2 = text2.strip('\xa0')
    print('Saves:', text1, '|', percent1, '|', text2, '|', percent2)
    

    结果:

    Possession: 58% | 42%
    Shots on Target: 8 of 15 | 53% | 2 of 4 | 50%
    Saves: 1 of 2 | 50% | 8 of 8 | 100%
    

    【讨论】:

    • 这很好用,甚至认为 find() 不是一个好的交换。谢谢弗拉斯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多