我想beautifulsoup蟒库,用于发展自己,我意识到我必须得到帮助。
import requests
from bs4 import BeautifulSoup
url = "https://www.basketball-reference.com/players/j/jamesle01.html"
r = requests.get(url)
soup = BeautifulSoup(r.content,"html.parser")
data = soup.find_all("table",{"class":"row_summable sortable stats_table now_sortable"})
print(data)
您下载HTML是不完全一样的网页上显示的HTML。在某一点,而加载的网页,javascript中添加now_sortable
类表在浏览器中。
当您使用请求下载页面,Javascript代码永远不会执行,因此,你没有now_sortable
类在你的桌子,这就是为什么你找不到的元素。
试着改变你的代码:
data = soup.find_all("table",{"class":"row_summable sortable stats_table"})
一般提示:使用请求下载文件时,请尝试保存您在本地请求,所以你可以有适当的看看它的页面:
with open('local_page.html', 'w', encoding='utf-8') as fout:
fout.write(r.text)
你可以只用Selenium来渲染页面,然后拉出HTML:
from selenium import webdriver
from bs4 import BeautifulSoup
url = "https://www.basketball-reference.com/players/j/jamesle01.html"
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html,"html.parser")
data = soup.find_all("table",{"class":"row_summable sortable stats_table now_sortable"})
print(data)