BS4在Python中找不到元素 [英] BS4 Not Locating Element in Python

查看:177
本文介绍了BS4在Python中找不到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python有点陌生,无法终生了解为什么以下代码没有提取我要获取的元素.

I am somewhat new to Python and can't for the life of me figure out why the following code isn’t pulling the element I am trying to get.

当前返回:

for player in all_players:

    player_first, player_last = player.split()
    player_first = player_first.lower()
    player_last = player_last.lower()
    first_name_letters = player_first[:2]
    last_name_letters = player_last[:5]

    player_url_code = '/{}/{}{}01'.format(last_name_letters[0], last_name_letters, first_name_letters)
    player_url = 'https://www.basketball-reference.com/players' + player_url_code + '.html'
    print(player_url) #test
    req = urlopen(player_url)
    soup = bs.BeautifulSoup(req, 'lxml')
    wrapper = soup.find('div', id='all_advanced_pbp')
    table = wrapper.find('div', class_='table_outer_container')


    for td in table.find_all('td'):
        player_pbp_data.append(td.get_text())

当前返回:

--> for td in table.find_all('td'):
        player_pbp_data.append(td.get_text()) #if this works, would like to 

AttributeError: 'NoneType' object has no attribute 'find_all'

注意:遍历包装对象的子对象将返回:

Note: iterating through children of the wrapper object returns:

< div class="table_outer_container" >作为树的一部分.

谢谢!

推荐答案

确保table包含您期望的数据.

Make sure that table contains the data you expect.

例如 https://www.basketball-reference.com/players/a/abdulka01.html 似乎不包含带有id='all_advanced_pbp'

这篇关于BS4在Python中找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆