如何基于python技术排除股票

时间:2019-06-09 04:02:16

标签: python beautifulsoup finance

我有一个代码,可以向我提供雅虎股票的技术信息,没问题,但是我试图使程序在不符合要求的情况下(例如,如果收入不超过100B。

我在这段代码的各个部分都尝试过if语句,但似乎没有一个起作用。

    technicals = {}
    try:
        url = ('http://finance.yahoo.com/q/ks?s='+stock)
        page = urllib2.urlopen(url)
        soup = BeautifulSoup(page, 'html.parser')
        tables = soup.findAll('table', {"class" : 'table-qsp-stats'})    # Found using page inspection
        for table in tables:
            table_body = table.find('tbody')
            rows = table_body.find_all('tr')

            for row in rows:
                col_name = row.find_all('span')                            # Use span to avoid supscripts
                col_name = [cell.text.strip() for cell in col_name]
                col_val = row.find_all('td')
                col_val = [cell.text.strip() for cell in col_val]
                technicals[col_name[0]] = col_val[1]                    # col_val[0] is the name cell (with subscript)
        return technicals
    except Exception as e:
        print('Failed, exception: ', str(e))



def scrape(stock_list, interested, technicals):
    for each_stock in stock_list:
        technicals = scrape_yahoo(each_stock)
        if int('Revenue') > 100000000000:
            print(each_stock)
            for ind in interested:
                print(ind + ": "+ technicals[ind])
            print("------")
            time.sleep(1)                                                    # Use delay to avoid getting flagged as bot
    return technicals

def main():
    stock_list = ['aapl', 'tsla', 'ge']
    interested = ['Market Cap (intraday)', 'Return on Equity', 'Revenue', 'Quarterly Revenue Growth']
    technicals = {}
    tech = scrape(stock_list, interested, technicals)
    print(tech)


main()

ValueError:以10为底的int()无效文字:“收入”

2 个答案:

答案 0 :(得分:0)

我假设technical变量是dict,并且具有Revenue键。 您应该从

更改
if  int('Revenue') 

if  int(technical.get('Revenue',0))

答案 1 :(得分:0)

import time
import urllib.request
from bs4 import BeautifulSoup
def scrape_yahoo(stock):
    technicals = {}
    try:
        url = ('http://finance.yahoo.com/q/ks?s= ' +stock)
        page = urllib.request.urlopen(url)
        soup = BeautifulSoup(page, 'html.parser')
        tables = soup.findAll('table', {"class" : 'table-qsp-stats'})    # Found using page inspection
        for table in tables:
            table_body = table.find('tbody')
            rows = table_body.find_all('tr')

            for row in rows:
                col_name = row.find_all('span')                            # Use span to avoid supscripts
                col_name = [cell.text.strip() for cell in col_name]
                col_val = row.find_all('td')
                col_val = [cell.text.strip() for cell in col_val]
                technicals[col_name[0]] = col_val[1]                    # col_val[0] is the name cell (with subscript)
        return technicals
    except Exception as e:
        print('Failed, exception: ', str(e))


def scrape(stock_list, interested, technicals):
    for each_stock in stock_list:
        technicals = scrape_yahoo(each_stock)

        if (float(technicals.get('Revenue',0).replace("B","")))*1000000000 > 100000000000:
            print(each_stock)
            for ind in interested:
                print(ind + ": "+ technicals[ind])
            print("------")
            time.sleep(1)                                                    # Use delay to avoid getting flagged as bot
    return technicals

def main():
    stock_list = ['aapl', 'tsla', 'ge']
    interested = ['Market Cap (intraday)', 'Return on Equity', 'Revenue', 'Quarterly Revenue Growth']
    technicals = {}
    tech = scrape(stock_list, interested, technicals)
    print(tech)


main()