【发布时间】:2021-02-15 10:18:02
【问题描述】:
我的代码输出:
<Response [200]>
https://www.tradingview.com/symbols/NSE-RELIANCE
1
[<div class="tv-symbol-price-quote__value js-symbol-last"></div>]
Process Finished with exit code 0
我正在尝试从 Excel 文件中读取股票名称,并使用此代码在我的 Excel 表中更新它们的最新价格。运行此代码后,我得到了一个数组(数据)中的输出,但它不包含任何股票价格,我以后可以在我的列表中检索和更新。有人可以帮我吗?The html tag highlighted containing the price of stock
import openpyxl
import time
import requests
from bs4 import BeautifulSoup
wb = openpyxl.load_workbook("try.xlsx")
sheets = wb.sheetnames
#print(sheets)
sh = wb['Stock']
data = sh['F4'].value
#print(data)
s_row = 4
s_col = 6
c_list = []
while sh.cell(row=s_row, column= s_col).value != None:
c_name = sh.cell(row=s_row, column=s_col).value
c_list.append(c_name)
s_row += 1
#print("Company name available in Database")
#[print(' ->', name) for name in c_list]
time.sleep(2)
for stock_symbol in c_list:
url = 'https://www.tradingview.com/symbols/NSE-' + stock_symbol
response = requests.get(url)
print(response)
print(url)
soup = BeautifulSoup(response.text, 'html.parser')
#print(soup)
data = soup.find_all('div', attrs={'class': 'tv-symbol-price-quote__value js-symbol-last'})
print(len(data))
print(data)
【问题讨论】:
-
看网站,好像是运行脚本填写的数据。
requests模块不运行脚本,因此您无法通过这种方式获得所需的数据。您将需要使用可以运行脚本的东西,例如selenium或查找备用数据源 -
只使用yfinance库
标签: python web-scraping finance stock