从BSE网站提取数据

2024-05-14 12:59:42 发布

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

如何提取安全ID、安全代码、组/索引的值,平均Wtd价格,交易日期,交易数量,可交付数量的百分比,使用Python3将其保存到XLS文件中。下面是链接。在

https://www.bseindia.com/stock-share-price/smartlink-network-systems-ltd/smartlink/532419/

附言:我对python完全陌生。我知道很少有libs能让报废变得更容易,比如beauthoulsoup、selenium、requires、lxml等等,对它们没有太多的了解。在

编辑1: 我试过了

from bs4 import BeautifulSoup
import requests
URL = 'https://www.bseindia.com/stock-share-price/smartlink-network-systems-ltd/smartlink/532419/'
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
table = soup.find('div', attrs = {'id':'newheaddivgrey'})
print(table)

它的输出是None。我期待网页中的所有表格,并进一步过滤,以获得所需的数据。在

^{pr2}$

尝试了另一个代码。同样的问题。在

编辑2: 试过硒。但我没有拿到表上的东西。在

from selenium import webdriver
driver = webdriver.Chrome(r"C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\bin\chromedriver.exe")
driver.get('https://www.bseindia.com/stock-share-price/smartlink-network-systems-ltd/smartlink/532419/')
table=driver.find_elements_by_xpath('//*[@id="SecuritywiseDeliveryPosition"]/table/tbody/tr/td/table/tbody/tr[1]/td')
 print(table)
driver.quit()

输出是[<selenium.webdriver.remote.webelement.WebElement (session="befdd4f01e6152942c9cfc7c563a6bf2", element="0.13124528538297953-1")>]


Tags: httpsimportcomsharewwwdriverseleniumstock
1条回答
网友
1楼 · 发布于 2024-05-14 12:59:42

在用Selenium加载页面后,可以使用driver.page_source获得Javascript修改的页面源代码。然后您可以在beauthoulGroup对象中传递这个页面源代码。在

driver = webdriver.Chrome()
driver.get('https://www.bseindia.com/stock-share-price/smartlink-network-systems-ltd/smartlink/532419/')
html = driver.page_source
driver.quit()

soup = BeautifulSoup(html, 'lxml')
table = soup.find('div', id='SecuritywiseDeliveryPosition')

这段代码将为您提供table变量中的Securitywise Delivery Position表。然后,您可以解析这个BeautifulSoup对象以获得所需的不同值。在

soup对象包含包含动态添加的元素的整页源。现在,您可以解析它来获得您提到的所有内容。在

相关问题 更多 >

    热门问题