简体   繁体   中英

FindNextSibling() function not working properly

I have tried the following code and this function doesn't work giving me an error.

"AttributeError: 'NoneType' object has no attribute 'findNextSiblings'"

What can I do solve this error ?

I tried removing the h_span , w_span variables and calling the soup.findNextSibling function in the loop instead of h_span.findNextSibling and it just returns an empty string , code does work.

from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import os

driver = webdriver.Chrome(executable_path= r'E:/Summer/FirstThings/Web scraping (bucky + pdf)/webscraping/tutorials-master/chromedriver.exe')
url = 'https://www.nba.com/players/aron/baynes/203382'
driver.get(url)

soup = BeautifulSoup(driver.page_source , 'lxml')

height = ''
h_span = soup.find('p', string = 'HEIGHT')
for span in h_span.findNextSiblings():
    height = height + span.text

weight = ''
w_span = soup.find('p', string = 'WEIGHT')
for span in w_span.findNextSiblings():
    weight = weight + span.text

born = ''
b_span = soup.find('p', string = 'BORN')
for span in b_span.findNextSiblings():
    born = born + span.text


print(height)
print("")
print(weight)
print("")
print(born)


driver.__exit__()

It should return the player height weight and born info in text format with the titles itself.

I love working with sports data!

You're doing way too much work here. No need to use Selenium or BeautifulSoup to parse the html as nba.com offers this data in a nice json format. All you'd need to do is find the players that you need within and pull out the data you want:

from bs4 import BeautifulSoup     
import requests

url = 'https://data.nba.net/prod/v1/2018/players.json'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}


jsonData = requests.get(url).json()

find_player = 'Baynes'

for player in jsonData['league']['standard']:
    if player['lastName'] == find_player:
        name = player['firstName'] + ' ' + player['lastName']
        height = player['heightFeet']  + 'ft ' + player['heightInches'] + 'in'
        weight = player['weightPounds'] + 'lbs'
        born = player['dateOfBirthUTC']

        print ('%s\n%s\n%s\n%s\n' %(name, height, weight, born))

Output:

Aron Baynes
6ft 10in
260lbs
1986-12-09

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM