这里是 Bs4 菜鸟。尝试了多种方法来使其正常工作,但现在我很困惑。
在尝试解析此页面时:https://www.basketball-reference.com/teams/NYK/2021.html
我正在使用以下代码查找特定表格
from urllib.request import urlopen
from bs4 import BeautifulSoup
year = 2021
team = "NYK"
team_url = f"https://www.basketball-reference.com/teams/{team}/{year}.html"
html = urlopen(team_url)
soup = BeautifulSoup(html, 'html.parser')
tbl = soup.find('table', {'id': 'team_misc'})
print(tbl)
我的输出是一个空列表 []
当我检查页面时,id 为 team_misc 的表存在。我正在用自己的眼睛看着它。然而我的代码什么都不返回。有什么明显的原因吗?由于时间关系,我不会列出我尝试过的所有内容,但如果提出建议,我会说我是否尝试过。
再次感谢!
答案 0 :(得分:0)
这将获得您确定的表格。您需要将 chromedriver.exe 下载到您的目录中或提供正确的路径。
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
year = 2021
team = "NYK"
team_url = f"https://www.basketball-reference.com/teams/{team}/{year}.html"
driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)
driver.get(team_url)
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, "html.parser")
tbl = soup.find('table', {'id': 'team_misc'})
print(tbl)
答案 1 :(得分:0)
由于您要查找的表位于 HTML comment
内,因此可能的解决方案是解析这些元素,并在找到匹配的 id
时返回。
from urllib.request import urlopen
from bs4 import BeautifulSoup, Comment #import the Comment object
year = 2021
team = "NYK"
team_url = f"https://www.basketball-reference.com/teams/{team}/{year}.html"
html = urlopen(team_url)
soup = BeautifulSoup(html, 'html.parser')
comments = soup.find_all(string=lambda text: isinstance(text, Comment))
for c in comments:
ele = BeautifulSoup(c.strip(), 'html.parser')
if tbl := ele.find("table"):
if (tbl_id := tbl.get("id")) == "team_misc":
print(tbl)