【发布时间】:2020-07-06 06:08:17
【问题描述】:
首先我会说我已经看到了类似的问题,但没有一个解决方案对我有用
所以我在我的 html 页面中寻找一个特定的类,但我总是得到一个 None 值返回。我在这里看到了一些描述相同问题的帖子,但没有一个解决方案对我有用。这是我的尝试 - 我正在寻找带有名字的玩家标签,即'Chase Young'
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import requests
url = "https://www.nfl.com/draft/tracker/prospects/allPositions?
college=allColleges&page=1&status=ALL&year=2020"
soup = BeautifulSoup(url.content, 'lxml')
match = soup.find('div', class_ = 'css-gu7inl')
print(match)
# Prints None
我尝试了另一种方法来查找匹配项,仍然返回无:
match = soup.find("div", {"class": "css-gu7inl"} # Print match is None
html 文件似乎不包含所有网页,所以我尝试使用 selenium,因为我在类似帖子中看到过推荐,但仍然一无所获:
driver = webdriver.Chrome("chromedriver")
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'lxml')
items=soup.select(".css-gu7inl")
print(items) # Empty list
我在这里做错了什么?
【问题讨论】:
-
我检查了 selenium 方法,它确实给出了您正在寻找的结果,您最终面临什么问题?
-
我不知道为什么它对你有用。我重新运行了代码,但我仍然得到一个空列表
-
我想下面的答案应该可以工作,基本上你需要等到浏览器加载所有内容,然后你需要解析 HTML 以从中获取内容。可能是当您从代码中点击时,它仍在加载内容,这就是为什么您没有得到 div 的结果。
标签: python selenium xpath beautifulsoup python-requests