首页 > 解决方案 > Beautifulsoup 按其类名提取 div

问题描述

我正在尝试从 html 页面中提取表格,

以下是我尝试过的代码,

driver = webdriver.Chrome(executable_path='C:/Users/krish/Desktop/Python/chromedriver.exe')
driver.get('https://www.marketsmojo.com/Stocks?StockId=592009&Exchange=0#navQuality')
driver.implicitly_wait(50)
soup = BeautifulSoup(driver.find_element_by_id("table1 comp2-table ng-tns-c94-1"),'lxml')
tables = soup.find_all("div", {"class": "card ng-tns-c94-1"})
hdfc_mm = pd.read_html(str(tables))

我正在尝试从提到的网站的质量菜单栏中提取一个表格。之后我通过在表格上使用检查来命名,并找到带有 divsion 类的表格名称。

但我收到以下错误,

无法定位元素:{"method":"css selector","selector":"[id="table1 comp2-table ng-tns-c94-1"]"}

在此处输入图像描述

突出显示所需的表。

如何找到表?请有任何想法。

谢谢

标签: pythonweb-scrapingbeautifulsoup

解决方案


这来自一个 API:

GET https://frapi.marketsmojo.com/stocks_quality/cardinfo?std={id}

它需要来自 url 查询参数的 id:

import requests

sid = "592009"

r = requests.get(f"https://frapi.marketsmojo.com/stocks_quality/cardinfo?sid={sid}")
print(r.json()["data"]["quality_tbl"]["list"])

在 repl.it 上试试这个


推荐阅读