Web Scraping Yahoo Finance 尝试不起作用

Web Scraping Yahoo Finance attempt not working

提前感谢那些花时间阅读我的问题的人!

我正在尝试从 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( )]) 也就是运行进入的问题。

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

背景信息:股票 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()

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

    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() return float 而不是字符串,因为你会在 findpg() 函数中出错