我试图从csv文件中提取一个股票列表,将每个股票代码上传到finviz.com网站,并将数据导出到csv文件。我是Python编程新手,但我知道这对我和其他人都有帮助。这就是我目前得到的。你知道吗
import csv
import urllib.request
from bs4 import BeautifulSoup
with open('shortlist.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
name = None
for row in reader:
if row[0]:
name = row[0]
print(name)
write_header = True
sauce = print(name)
soup = BeautifulSoup(sauce.text, 'html.parser')
print(soup.title.text)
symbols = name
""""
print(symbols)
"""
URL_BASE = "https://finviz.com/quote.ashx?t="
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
for ticker in symbols:
URL = URL_BASE + ticker
try:
fpage = urllib.request.urlopen(URL)
fsoup = BeautifulSoup(fpage, 'html.parser')
if write_header:
# note the change
writer.writerow(['ticker'] + list(map(lambda e: e.text, fsoup.find_all('td', {'class': 'snapshot-td2-cp'}))))
write_header = False
# note the change
writer.writerow([ticker] + list(map(lambda e: e.text, fsoup.find_all('td', {'class': 'snapshot-td2'}))))
except urllib.request.HTTPError:
print("{} - not found".format(URL))
我丢失了csv文件的输出“输出.csv". 我只看到输入csv文件“shortlist”中的数据。领带或链接没有正确连接。我花了几个星期研究/研究如何连接。非常感谢你的帮助。你知道吗
这是输出: enter image description here
相关问题 更多 >
编程相关推荐