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

2024-04-19 05:49:39 发布

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

我试图从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”中的数据。领带或链接没有正确连接。我花了几个星期研究/研究如何连接。非常感谢你的帮助。你知道吗


Tags: 文件csvtextnameimporturlrequesturllib
1条回答
网友
1楼 · 发布于 2024-04-19 05:49:39
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:

这是输出: enter image description here

相关问题 更多 >