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)
能否請您解釋一下什麼結果你得到? –
我在[u'Nestl \ xe9 SA,Registered'] \t ['5.3%']的單獨欄目中獲得了價值,但我需要價值「雀巢SA,註冊\t 5.3%」與上述參考網站相同。 它的價值還需要「雀巢」編碼。請在此幫助@Anand S Kumar – magic
查看下面的答案。 –