2013-10-31 72 views
1

我正在使用以下代碼寫入一個csv文件。Python:BeautifulSoup Findall跳轉到下一個標記

import urllib2 
from BeautifulSoup import BeautifulSoup 
import csv 
import re 

page = urllib2.urlopen('http://finance.yahoo.com/q/ks?s=F%20Key%20Statistics').read() 

f = csv.writer(open("pe_ratio.csv","wb")) 
f.writerow(["Name","PE","Revenue % YOY","ROA% YOY","OCF Positive","Debt - Equity"]) 

soup = BeautifulSoup(page) 
all_data = soup.findAll('td', "yfnc_tabledata1") 
f.writerow(('Ford', all_data[2].getText())) 



name_company = soup.findAll("div", {"class" : "title"}) 
# find all h2 

#print soup.prettify 

#h2 div class="title" 

print name_company 

我已經找到我想要的東西擺在csv文件,但現在我需要限制它只是,「福特汽車公司(F)當我打印出來name_company我得到這個:

[<div class="title"><h2>Ford Motor Co. (F)</h2>  <span class="rtq_exch"> <span    class="rtq_dash">-</span>NYSE  </span><span class="wl_sign"></span></div>] 

我嘗試過使用name_company.next和name_company.content [0]。什麼會工作?name_company使用findall,我不知道是否使.content和.next爲空。感謝您提前給予幫助。

回答

2

使用find()獲取下一個<h2>標籤並使用string讀取其文本節點。

name_company = soup.findAll("div", {"class" : "title"}) 
for name in name_company: 
    print name.find('h2').string 

UPDATE:見註釋。

for name in name_company: 
    ford = name.find('h2').string 
    f.writerow([ford, all_data[2].getText()]) 

它產生:

Name,PE,Revenue % YOY,ROA% YOY,OCF Positive,Debt - Equity 
Ford Motor Co. (F),11.23 
+0

感謝。我將如何編寫在這行代碼中打印以代替「Ford」的內容? f.writerow(('Ford',all_data [2] .getText())) –

+0

@RobertBirch:我已經更新了答案。 – Birei