简体   繁体   English

创建一个python脚本,该脚本将读取csv文件并使用该输入来从finviz.com抓取数据然后将数据导出到csv文件

[英]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.

相关问题 无法从CSV文件导入数据,遍历代码,从finviz.com抓取数据并导出到CSV文件以进行更多分析 - Trouble Inporting data from a csv file, loop through the tickers, scrape data from finviz.com, and export to a csv file for more analysis 如何从finviz.com获取和分析历史市场数据 - How to acquire and analyze historical market data from finviz.com 如何将数据从BeautifulSoup抓取文件导出到CSV文件 - How to export data from a beautifulsoup scrape to a csv file 如何使用python抓取多页网站并将数据导出到.csv文件? - how to scrape multipage website with python and export data into .csv file? 我如何使用 Python 和 ZC2ED0329D2D3CF54C78317B209D7C0D50DC67845692BEC50Z 从 Finviz.com 刮取最大赢家和最大输家的表格行 - How can i scrape table rows for Top Gainers and Top Losers from Finviz.com with Python and BeautifulSoup or Pandas? Python将csv数据导出到文件中 - Python export csv data into file 将python数据导出到csv文件 - export python data to csv file 将输入数据读入 csv 文件 - Read input data to csv file web 将数据抓取到 python 上的 csv 文件,以及抓取链接的代码 - web scraping data to csv file on python, and the code to scrape a link 试图从Python抓取数据中制作一个CSV文件 - trying to make a csv file from scrape data from Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM