如何根据给定的输入从web提取数据到excel?

2024-04-19 21:14:14 发布

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

基于excel的时间段提取数据并保存。
这是链接stock market info
以下是快照
enter image description here 在网页“下载csv格式的文件”中有一个选项,但是我想自动化这个过程,因为我需要大约100个公司的数据。在

我尝试了excel中的web查询选项,但是上面提到的链接没有显示任何表格符号供我导入。在

我只想从一些伪代码开始。一开始也有可能吗?在

提前谢谢!在


Tags: 文件csv数据info网页链接过程格式
1条回答
网友
1楼 · 发布于 2024-04-19 21:14:14

希望这有帮助。在

import requests
from bs4 import BeautifulSoup

symbol = "Company name"
from_date = "19-12-2016"
to_date = "20-12-2016"
URL = "https://www.nseindia.com/products/dynaContent/common/productsSymbolMapping.jsp?symbol=%s&segmentLink=3&symbolCount=3&series=ALL&dateRange=+&fromDate=%s&toDate=%s&dataType=PRICEVOLUMEDELIVERABLE" % (symbol,from_date,to_date)

r = requests.get(URL,verify=false)
bso = BeautifulSoup(r.text,'html.parser')

outfile = symobl+'_'+from_date+'_'+to_date+'.csv'
data = ''

for tag in bso.find_all('th'):
    data += tag.text.split()+','
data = data[:-1]+'\n'

for row in bso.find_all('tr')[1:]:
    for field in row.find_all('td'):
        data += field.text.split()+','
    data = data[:-1]+'\n'

with open(outfile,'w') as f:
    f.write(data)

相关问题 更多 >