2015-09-08 52 views
2

我的代碼

from lxml import html 
import requests 
import csv 
# encoding=utf8 
import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 


# example site 
page = requests.get('http://www.wintergreenfund.com/reports/top-ten/') 
tree = html.fromstring(page.text) 
#This will create a list of services: 

tname = tree.xpath('//*[@id="colLeft"]//table//tr/td[1]/text()') 
tvalue = tree.xpath('//table//tr/td[2]/text()') 



print tname 
print tvalue 

print 'Input the csv file' 
csvfile = raw_input("> ") 

res = tname,tvalue 


#Assuming res is a list of lists 
with open(csvfile, "w") as output: 
    writer = csv.writer(output, lineterminator='\n') 
    writer.writerows(res) 

我在CSV輸出

雷諾美國公司的合併,陸友公司英美菸草蟒蛇給列名和單獨列表寫入值

8.30 %7.50%7.10%6.60%6.40%5.90%5.30%4.80%4.70%4.10%

需要的輸出與網站中的庫侖名稱相同

參考http://www.wintergreenfund.com/reports/top-ten/

而且也統一不工作。需要幫助這個

我的新代碼

from lxml import html 
import requests 
import csv 

page = requests.get('http://www.wintergreenfund.com/reports/top-ten/') 
tree = html.fromstring(page.text) 

csvrows = [] 
for rows in tree.xpath('//*[@id="colLeft"]//table//tr'): 
    csvrows.append([rows.xpath('./td[1]/text()'),rows.xpath('./td[2]/text()')]) 
print csvrows 
print 'Input the csv file' 
csvfile = raw_input("> ") 
with open(csvfile, "w") as output: 
    writer = csv.writer(output, lineterminator='\n') 
    writer.writerow(['Name','Value']) #substitute as appropriate. 
    writer.writerows(csvrows) 

我在它獲得價值與[」「],也空[]

+0

能否請您解釋一下什麼結果你得到? –

+0

我在[u'Nestl \ xe9 SA,Registered'] \t ['5.3%']的單獨欄目中獲得了價值,但我需要價值「雀巢SA,註冊\t 5.3%」與上述參考網站相同。 它的價值還需要「雀巢」編碼。請在此幫助@Anand S Kumar – magic

+0

查看下面的答案。 –

回答

1

首先,如果你想在每個相應的索引處合併兩個列表,你應該使用zip(),目前你正在創建一個包含兩個列表的元組 - res = tname,tvalue - 然後將它原樣寫入csv。另外,其次,你應該首先使用xpath來獲取表中的每一行,然後使用xpath從它中獲取每個所需的td元素。而不是像當前使用的那樣使用兩個xpath。

示例 -

from lxml import html 
import requests 
import csv 

page = requests.get('http://www.wintergreenfund.com/reports/top-ten/') 
tree = html.fromstring(page.text) 

csvrows = [] 
for rows in tree.xpath('//*[@id="colLeft"]//table//tr'): 
    row1text = rows.xpath('./td[1]/text()') 
    row2text = rows.xpath('./td[2]/text()') 
    if row1text and row2text: 
     csvrows.append([row1text[0],row2text[0]]) 
print(csvrows) 
print('Input the csv file') 
csvfile = input("> ") 
with open(csvfile, "w") as output: 
    writer = csv.writer(output, lineterminator='\n') 
    writer.writerow(['Name','Value']) #substitute as appropriate. 
    writer.writerows(csvrows)