Python crawling: crawler

Time:2021-1-6

The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time

This article from Tencent cloud Author: lonely moon
在这里插入图片描述

Objective: to simply obtain the fund data of Tiantian fund website

在这里插入图片描述

Code area

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 'crawling page {J})
        Print (f 'this page crawls {len (LIST1)} data')
    
        num = []
        name = []
        today_price = []
        yesterday_price = []
        day_value = []
        day_value_rate = []
        subscription_status = []
        redemption_status = []
        service_charge = []

        for i in range(len(list1)):
          #1. Fund code number
            num.append(list1[i][0])
            #2. Stock name
            name.append(list1[i][1])
      #3. Today's net fund
            today_price.append(list1[i][3])
            #4. Yesterday's net fund
            yesterday_price.append(list1[i][5])
            #5. Daily growth
            day_value.append(list1[i][7])
            #6. Daily growth rate
            day_value_rate.append(list1[i][8])
            #7. Subscription status
            subscription_status.append(list1[i][9])
            #8. Redemption status
            redemption_status.append(list1[i][10])
            #9. Service charge
            service_charge.append(list1[i][17])

        df = pd.DataFrame()
        DF ['fund code '] = num
        DF ['fund name '] = name
        DF ['2020-08-12 '] = today_ price
        DF ['2020-08-11 '] = yesterday_ price
        DF ['daily growth '] = day_ value
        DF ['daily growth rate '] = day_ value_ rate
        DF ['subscription status'] = subscription_ status
        DF ['redemption status'] = redemption_ status
        DF ['handling charge '] = Service_ charge

        try:
            df.to_ Excel (f 'fund {J}. Xlsx','fund information ', index = none, encoding ='utf-8'))

        except Exception as e:
            print(e)

    time.sleep(1)

 

effect

在这里插入图片描述
在这里插入图片描述