【问题标题】:Extract data from BSE website从 BSE 网站提取数据
【发布时间】:2023-04-05 08:04:01
【问题描述】:

如何使用 Python 3 提取证券 ID、证券代码、组/指数、Wtd.Avg 价格、交易日期、交易数量、可交付数量占交易数量的百分比并将其保存到 XLS 文件中。下面是链接。

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

PS:我对 python 完全陌生。我知道像 BeautifulSoup、selenium、requests、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。我期待网页中的所有表格并进一步过滤它们以获得所需的数据。

import requests
import lxml.html
URL = 'https://www.bseindia.com/stock-share-price/smartlink-network-systems-ltd/smartlink/532419/'
r = requests.get(URL)
root = lxml.html.fromstring(r.content)
title = root.xpath('//*[@id="SecuritywiseDeliveryPosition"]/table/tbody/tr/td/table/tbody/tr[1]/td')
print(title)

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

编辑 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")>]

【问题讨论】:

  • 欢迎来到 Stack Overflow!请edit您的问题显示the code you have so far。您应该至少包含您遇到问题的代码的大纲(但最好是minimal reproducible example),然后我们可以尝试帮助解决具体问题。您还应该阅读How to Ask
  • 在原帖中添加代码。
  • requests 库最适合单个 HTTP 请求。它不会下载整个网页,只会下载请求位置的 HTML 文件;这意味着不会下载任何其他内容,例如由 Javascript 加载的内容。打开提供的 URL 时,会瞬间出现加载信息,提示实际数据确实是 JS 加载的。考虑查看网站的 JS 并对其进行逆向工程以直接获取您需要的数据,而不是从呈现的页面中抓取它。
  • 你能帮我处理一小段废弃javascript数据的代码吗?请使用我帖子中提供的链接。

标签: python python-3.x selenium beautifulsoup python-requests


【解决方案1】:

使用 Selenium 加载页面后,您可以使用 driver.page_source 获取 Javascript 修改的页面源。然后,您可以在 BeautifulSoup 对象中传递此页面源。

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 对象包含完整的页面源代码,包括动态添加的元素。现在,您可以解析它以获取您提到的所有内容。

【讨论】:

    猜你喜欢
    • 2023-01-16
    • 2018-04-03
    • 1970-01-01
    • 2016-02-27
    • 2015-08-26
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多