Python 从BSE网站提取数据

Python 从BSE网站提取数据,python,python-3.x,selenium,beautifulsoup,python-requests,Python,Python 3.x,Selenium,Beautifulsoup,Python Requests,如何使用Python 3提取证券ID、证券代码、组/索引、Wtd.Avg价格、交易日期、交易量、%的可交付量到交易量的值,并将其保存到XLS文件中。下面是链接 附言:我对python一无所知。我知道很少有像BeautifulSoup、selenium、requests、lxml等使报废更容易的LIB。我对它们不太了解 编辑1: 我试过了 from bs4 import BeautifulSoup import requests URL = 'https://www.bseindia.com/s

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

附言:我对python一无所知。我知道很少有像BeautifulSoup、selenium、requests、lxml等使报废更容易的LIB。我对它们不太了解

编辑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加载页面后,您可以使用
驱动程序。page\u 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')
此代码将在
变量中为您提供Securitywise交货位置表。然后可以解析此BeautifulSoup对象以获得所需的不同值


soup
对象包含完整的页面源代码,包括动态添加的元素。现在,您可以对其进行解析以获得您提到的所有内容。

欢迎使用堆栈溢出!请出示你的问题。您应该至少包括您遇到问题的代码的大纲(但最好是a),然后我们可以尝试帮助解决特定问题。您还应该阅读。在原始帖子中添加了代码。
请求
库最适合单个HTTP请求。它不会下载整个网页,只下载请求位置的HTML文件;这意味着不会下载任何其他内容,例如Javascript加载的内容。打开提供的URL时,会立即出现一条加载消息,表明实际数据确实是由JS加载的。考虑浏览站点的JS并逆向工程它来直接获取你需要的数据,而不是将它从一个已渲染的页面中抹去。你能帮我一小部分代码来删除JavaScript数据吗?请使用我帖子中的链接。