gpt4 book ai didi

Python Beautifulsoup (bs4) findAll 找不到所有元素

转载 作者:太空狗 更新时间:2023-10-30 02:51:36 24 4
gpt4 key购买 nike

从代码中的 url,我最终试图从页面中收集所有玩家的名字。但是,当我使用 .findAll 来获取所有列表元素时,我还没有成功。请指教。

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

players_url = 'https://stats.nba.com/players/list/?Historic=Y'

# Opening up the Connection and grabbing the page
uClient = uReq(players_url)
page_html = uClient.read()

players_soup = soup(page_html, "html.parser")

# Taking all of the elements from the unordered lists that contains all of the players.

list_elements = players_soup.findAll('li', {'class': 'players-list__name'})

最佳答案

作为@Oluwafemi Sule建议最好将 seleniumBS 一起使用:

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://stats.nba.com/players/list/?Historic=Y')
soup = BeautifulSoup(driver.page_source, 'lxml')
for div in soup.findAll('li', {'class': 'players-list__name'}):
print(div.find('a').contents[0])

输出:

Abdelnaby, Alaa
Abdul-Aziz, Zaid
Abdul-Jabbar, Kareem
Abdul-Rauf, Mahmoud
Abdul-Wahad, Tariq

等等

关于Python Beautifulsoup (bs4) findAll 找不到所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55274450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com