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爲空。感謝您提前給予幫助。
感謝。我將如何編寫在這行代碼中打印以代替「Ford」的內容? f.writerow(('Ford',all_data [2] .getText())) –
@RobertBirch:我已經更新了答案。 – Birei