[英]Create a python script that will read a csv file and use that input to web scrape the data from finviz.com then export the data into a csv file
I'm trying to pull in a list of stocks from a csv file, upload each stock ticker into finviz.com, and export the data to csv file. 我正在尝试从csv文件中提取股票列表,将每个股票代码上传到finviz.com,然后将数据导出到csv文件。 I'm new to Python programing but I know this will help me and others. 我是Python编程的新手,但我知道这对我和其他人有帮助。 This is what I got so far. 这是我到目前为止所得到的。
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))
I'm missing the output on the csv file "output.csv". 我缺少csv文件“ output.csv”上的输出。 I'm only seeing the data from my input csv file "shortlist". 我只看到输入csv文件“shortlist”中的数据。 The tie or link is not correctly linked.I've spent a couple of weeks researching/working on how to do this. 领带或链接没有正确链接。我花了几周时间研究/研究如何做到这一点。 You're help is greatly appreciated. 非常感谢您的帮助。
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:
This is the output: enter image description here 这是输出: 在此输入图像描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.