在“https://money.cnn.com/data/markets/nasdaq/”抓取纳斯达克成分股数据并将数据表存到一个Excel表格中。
import requests
from lxml import etree
import xlwt
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet('info')
sheet.write(0,0,'company')
sheet.write(0,1,'price')
sheet.write(0,2,'change')
sheet.write(0,3,'%change')
sheet.write(0,4,'P/E')
sheet.write(0,5,'volume')
sheet.write(0,6,'YTDchange')
url = "https://money.cnn.com/data/markets/nasdaq/"
res = requests.get(url)
#print(html.status_code)
content = res.content
html = etree.HTML(content)
companys = html.xpath('//*[@class="wsod_symbol"]/text()')
prices = html.xpath('//tbody/tr/td[2]//text()')
changes = html.xpath('//tbody/tr/td[3]//text()')
_changes = html.xpath('//tbody/tr/td[4]//text()')
PE = html.xpath('//tbody/tr/td[5]//text()')
volume = html.xpath('//tbody/tr/td[6]//text()')
YTDchange = html.xpath('//tbody/tr/td[7]//text()')
i = 1
for c,p,ch,_ch,pe,v,yc in zip(companys,prices,changes,_changes,PE,volume,YTDchange) :
sheet.write(i,0,c)
sheet.write(i,1,p)
sheet.write(i,2,ch)
sheet.write(i,3,_ch)
sheet.write(i,4,pe)
sheet.write(i,5,v)
sheet.write(i,6,yc)
i += 1
workbook.save('comp.xls')