当我试图请求数据时,它返回none。我该怎么修?

2024-05-13 01:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试开发自己的python库,我意识到我必须得到帮助。你知道吗

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)

Tags: fromhttpsimporturldatahtmlwwwtable
2条回答

您可以使用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)

您下载的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)

相关问题 更多 >