Python 使用Selenium&;从网站数据获取动态表格;靓汤

Python 使用Selenium&;从网站数据获取动态表格;靓汤,python,selenium,beautifulsoup,Python,Selenium,Beautifulsoup,我正在尝试使用selenium和chrome驱动程序从网站获取动态表格数据。在获取数据方面需要一些帮助,因为我现在获取的是空数据框。您离获取该表还有一个请求。它以普通的JSON形式出现,因此您只需要请求 例如: from selenium import webdriver from bs4 import BeautifulSoup path = 'C:/Program Files/Google/Chrome/Application/ChromeDriver' driver = webdriver

我正在尝试使用selenium和chrome驱动程序从网站获取动态表格数据。在获取数据方面需要一些帮助,因为我现在获取的是空数据框。

您离获取该表还有一个请求。它以普通的
JSON
形式出现,因此您只需要
请求

例如:

from selenium import webdriver
from bs4 import BeautifulSoup
path = 'C:/Program Files/Google/Chrome/Application/ChromeDriver'
driver = webdriver.Chrome(executable_path = path)
driver.get("https://www1.nseindia.com/products/content/equities/equities/oi_spurts.htm")
soup = BeautifulSoup(driver.page_source,"lxml")
for items in soup.select('#profile table.table tr'):
    data = [item.get_text(strip=True) for item in items.select("th,td")]
    print(data)
输出:

import requests
from tabulate import tabulate

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0",
    "X-Requested-With": "XMLHttpRequest",
}
url = "https://www1.nseindia.com/live_market/dynaContent/live_analysis/oi_spurts/topPositiveOIChangeData.json"
response = requests.get(url, headers=headers).json()
print(tabulate(response["data"], headers="keys", showindex=False, tablefmt="pretty"))


你离那张桌子还有一个要求。它以普通的
JSON
形式出现,因此您只需要
请求

例如:

from selenium import webdriver
from bs4 import BeautifulSoup
path = 'C:/Program Files/Google/Chrome/Application/ChromeDriver'
driver = webdriver.Chrome(executable_path = path)
driver.get("https://www1.nseindia.com/products/content/equities/equities/oi_spurts.htm")
soup = BeautifulSoup(driver.page_source,"lxml")
for items in soup.select('#profile table.table tr'):
    data = [item.get_text(strip=True) for item in items.select("th,td")]
    print(data)
输出:

import requests
from tabulate import tabulate

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:82.0) Gecko/20100101 Firefox/82.0",
    "X-Requested-With": "XMLHttpRequest",
}
url = "https://www1.nseindia.com/live_market/dynaContent/live_analysis/oi_spurts/topPositiveOIChangeData.json"
response = requests.get(url, headers=headers).json()
print(tabulate(response["data"], headers="keys", showindex=False, tablefmt="pretty"))


这回答了你的问题吗?这回答了你的问题吗?