Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 整数、浮点和美丽的汤属性_Python_Int_Beautifulsoup - Fatal编程技术网

Python 整数、浮点和美丽的汤属性

Python 整数、浮点和美丽的汤属性,python,int,beautifulsoup,Python,Int,Beautifulsoup,我正在尝试将我的所有数据标记转换成一种格式,在这种格式中,我可以使用布尔值与它们进行比较。我认为这涉及到使用float和/或int运算符。然而,我有一些担心,因为输出一旦网站被废弃。输出为整数、小数和百分比。我所说的修改的具体行是第33行。我尝试过使用int()和.int。我在Stackoverflow或Beautiful Soup文档中没有发现任何关于此的问题 from BeautifulSoup import BeautifulSoup import csv import re import

我正在尝试将我的所有数据标记转换成一种格式,在这种格式中,我可以使用布尔值与它们进行比较。我认为这涉及到使用float和/或int运算符。然而,我有一些担心,因为输出一旦网站被废弃。输出为整数、小数和百分比。我所说的修改的具体行是第33行。我尝试过使用int()和.int。我在Stackoverflow或Beautiful Soup文档中没有发现任何关于此的问题

from BeautifulSoup import BeautifulSoup
import csv
import re
import urllib
import urllib2
from urllib2 import HTTPError
# import modules

symbolfile = open("symbols.txt")
symbolslist = symbolfile.read()
newsymbolslist = symbolslist.split("\n")

i = 0

f = csv.writer(open("pe_ratio.csv","wb"))
# short cut to write

f.writerow(["Name","PE","Revenue % Quarterly","ROA% YOY","Operating Cashflow","Debt to Equity"])
#first write row statement

# define name_company as the following
while i<len(newsymbolslist):
    try:
        page = urllib2.urlopen("http://finance.yahoo.com/q/ks?s="+newsymbolslist[i] +"%20Key%20Statistics").read()
    except urllib2.HTTPError:
        continue
    soup = BeautifulSoup(page)
    name_company = soup.findAll("div", {"class" : "title"}) 
    for name in name_company: #add multiple iterations?        
        all_data = soup.findAll('td', "yfnc_tabledata1")
        stock_name = name.find('h2').string #find company's name in name_company with h2 tag
        try:    
            f.writerow([stock_name, all_data[2].getText(),all_data[17].getText(),all_data[13].getText(), all_data[29].getText(),all_data[26].getText()]) #write down PE data
        except (IndexError, HTTPError) as e:
            pass
    i+=1    

请记住,通过在symbols.txt文件中垂直放置股票代码符号来加载股票代码符号。

如果要对数据进行比较(即季度百分比大于25),则必须格式化文本,以便将其转换为数字

quarterly_percent = all_data[17].getText()
if quarterly_percent != "N/A":
    #cut off the percent sign and conver to a "python number"
    quarterly_percent = float(quarterly_percent[:-1])
    if quarterly_percent > 25:
        print "its a good one"

要将所有数据字符串值转换为数字,请尝试以下操作:

all_data = soup.findAll('td', "yfnc_tabledata1")
stock_name = name.find('h2').string #find company's name in name_company with h2 tag

clean_data = list()
for x in [data.GetText().strip(' %') for data in all_data]
    try: 
        clean_data.append(float(x))
    except ValueError:
        clean_data.append(x)

try:    
    f.writerow([stock_name, clean_data[2], clean_data[17], clean_data[13], clean_data[29], clean_data[26]]) #write down PE data
except (IndexError, HTTPError) as e:
        pass

你的问题是什么?
all_data = soup.findAll('td', "yfnc_tabledata1")
stock_name = name.find('h2').string #find company's name in name_company with h2 tag

clean_data = list()
for x in [data.GetText().strip(' %') for data in all_data]
    try: 
        clean_data.append(float(x))
    except ValueError:
        clean_data.append(x)

try:    
    f.writerow([stock_name, clean_data[2], clean_data[17], clean_data[13], clean_data[29], clean_data[26]]) #write down PE data
except (IndexError, HTTPError) as e:
        pass