用爬虫爬取国泰安的夏普指数

# -*- coding: UTF-8 -*-
import urllib
import time
import pandas as pd
n = 001747
s = 0
wurl = 'http://fund.eastmoney.com/f10/tsdata_'
filename = 'sharp.txt'
data = pd.read_excel('834_4_5.xlsx', header=0)
def save_data(i,n):
    i = str(i).zfill(6)
    url = wurl + str(i) + '.html'
    print url
    content = urllib.urlopen(url).read().decode('utf-8')
    time.sleep(2)
    head = ("夏普比率").decode('utf-8')
    tail = ("</tr>").decode('utf-8')
    ph = content.find(head)
    ph1 = content.find('num',ph)
    pj1 = content.find('<',ph1)
    num1 = content[ph1+5:pj1]
    print num1
    ph2 = content.find('num',pj1)
    pj2 = content.find('<',ph2)
    num2 = content[ph2+5:pj2]
    print num2
    ph3 = content.find('num',pj2)
    pj3 = content.find('<',ph3)
    num3 = content[ph3+5:pj3]
    print num3
    if i == n:
        f.write(str(i)+','+num1+','+num2+','+num3)
    else:
        f.write(str(i)+','+num1+','+num2+','+num3+'\n')
with open(filename,'w') as f:
    for c in data['FundClassID']:
        c = c-202000000000
        if c != s:
            save_data(c,n)
        s = c
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值