网络抓取 Yahoo Finance 尝试无效

时间:2021-04-24 18:04:48

标签: python web-scraping finance stock yahoo-finance

预先感谢那些花时间阅读我的问题的人!

我试图从雅虎财经上获取一些信息,但在运行该程序时遇到了一些问题。

我遇到的问题是,在我尝试将值写入 csv 文件时的最后一行中,此错误消息不断弹出:

回溯(最近一次调用最后一次): 文件“...”,第 79 行,在 csv_writer.writerow([j.name(), j.price(), j.pricetarget()]) 类型错误:“str”对象不可调用

基本上是最后一行( csv_writer.writerow([j.name(), j.price(), j.pricetarget(), j.findpg()]) 遇到了问题。

我不确定网站上是否不存在特定值。为什么我的对象显示为字符串,从而导致代码无法调用对象的函数?嗯

背景信息:stock tickers.txt 是一个文件,我正在提取我的股票代码名称,以便解析和查找有关雅虎财经的相应页面和信息。

import requests
from bs4 import BeautifulSoup
import csv

csv_file = open('pricetarget.csv', 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Stock Ticker', 'Price', 'Price Target', 'Potential Gain'])

class ptfinder:
    def __init__(self, pturl, findpricetargettag, findpricetargetclass, priceurl, findpricetag, findpriceclass, nameurl, findnametag, findnameclass):
        self.pturl = pturl
        self.priceurl = priceurl
        self.findpricetag = findpricetag
        self.findpriceclass = findpriceclass
        self.nameurl = nameurl
        self.findnametag = findnametag
        self.findnameclass = findnameclass
        self.findpricetargettag = findpricetargettag
        self.findpricetargetclass = findpricetargetclass


    def name(self):
        self.source = requests.get(self.nameurl).text
        self.soup = BeautifulSoup(self.source, 'lxml')
        self.name = self.soup.find(self.findnametag, class_=self.findnameclass).text
        return self.name

    def price(self):
        self.source = requests.get(self.priceurl).text
        self.soup1 = BeautifulSoup(self.source, 'lxml')
        try:
            self.price = self.soup1.find(self.findpricetag, class_=self.findpriceclass).text
            return self.price
        except:
            print('h')
            return 'No Price Provided'

    def pricetarget(self):
        self.source = requests.get(self.pturl).text
        self.soup2 = BeautifulSoup(self.source, 'lxml')
        try:
            self.pricetarget = self.soup2.find(self.findpricetargettag, class_=self.findpricetargetclass).text
            return self.pricetarget
        except:
            return 'No Price Target Provided'


    def findpg(self):
        self.pg = str(((self.pricetarget() - self.price())/self.price())*100) + '%'
        return self.pg



stocksymbols = []
stockname = []
pt = []
textfile = open("/Users/ryanong/PycharmProjects/investing/stockksymbols.txt", 'r')
for line in textfile:
    shares = list(line.split(','))
    stocksymbols.append(shares[0])
    stockname.append(shares[1])

for i in range(50):
    checkstock = str(stocksymbols[i])
    nameurl = 'https://finance.yahoo.com/quote/' + checkstock + '/analysis?p=' + checkstock
    priceurl = 'https://finance.yahoo.com/quote/' + checkstock + '/analysis?p=' + checkstock
    pturl = 'https://finance.yahoo.com/quote/' + checkstock + '/analysis?p=' + checkstock
    s = ptfinder(
        pturl, 'span', "Trsdu(0.3s)",
        priceurl, 'span', 'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)',
        nameurl, 'h1', 'D(ib) Fz(18px)')
    pt.append(s)


for j in pt:
    csv_writer.writerow([j.name(), j.price(), j.pricetarget(), j.findpg()])

csv_file.close()

1 个答案:

答案 0 :(得分:0)

您正在混合方法名称和属性,例如:

    def pricetarget(self):                               # <--- method name is "pricetarget"
        self.source = requests.get(self.pturl).text
        self.soup2 = BeautifulSoup(self.source, "lxml")
        try:
            self.pricetarget = self.soup2.find(          # <--- here you're rewriting the method with string
                self.findpricetargettag, class_=self.findpricetargetclass
            ).text
            return self.pricetarget
        except:
            return "No Price Target Provided"

解决方案是重命名 def pricetarget(self) 或变量 self.pricetarget

另外,请确保 pricetarget()price() 返回 float 而不是字符串,因为您会在 findpg() 函数中出错