爬蟲 -- 天天基金網資料簡單爬取

2020-09-22 15:00:22

目標 — 簡單獲取天天基金網站的基金資料

![獲取天天基金網站中基金資料,(https://img-blog.csdnimg.cn/20200921190630689.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzAxNDAx,size_16,color_FFFFFF,t_70#pic_center)
進行翻頁獲取,共計48頁,並儲存到Excel工作簿中

程式碼區

import requests
import time
import pandas as pd

if __name__ == '__main__':
    for j in range(1, 49):
        url = f'http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?t=1&lx=1&letter=&gsid=&text=&sort=zdf,desc&page={j},200&dt=1597126258333&atfc=&onlySale=0'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER'
        }
        resp = requests.get(url, headers=headers).text
        str_ = resp[102:]
        list1 = eval(str_.split(",count")[0])
        print(f'正在爬取第{j}頁')
        print(f'本頁爬取{len(list1)}條資料')
		
        num = []
        name = []
        today_price = []
        yesterday_price = []
        day_value = []
        day_value_rate = []
        subscription_status = []
        redemption_status = []
        service_charge = []

        for i in range(len(list1)):
        	# 1、基金程式碼號
            num.append(list1[i][0])
            # 2、股票名稱
            name.append(list1[i][1])
			# 3、今日基金淨額
            today_price.append(list1[i][3])
            # 4、昨日基金淨額
            yesterday_price.append(list1[i][5])
            # 5、日增長值
            day_value.append(list1[i][7])
            # 6、日增長率
            day_value_rate.append(list1[i][8])
            # 7、申購狀態
            subscription_status.append(list1[i][9])
            # 8、贖回狀態
            redemption_status.append(list1[i][10])
            # 9、手續費
            service_charge.append(list1[i][17])

        df = pd.DataFrame()
        df['基金程式碼'] = num
        df['基金名稱'] = name
        df['2020-08-12\n單位淨值'] = today_price
        df['2020-08-11\n單位淨值'] = yesterday_price
        df['日增長值'] = day_value
        df['日增長率\n%'] = day_value_rate
        df['申購狀態'] = subscription_status
        df['贖回狀態'] = redemption_status
        df['手續費'] = service_charge

        try:
            df.to_excel(f'基金{j}.xlsx', '基金資訊', index=None, encoding='utf-8')

        except Exception as e:
            print(e)

    time.sleep(1)

效果

在這裡插入圖片描述
第48頁資料只有19條
在這裡插入圖片描述