```python

import random

Python编程:天天基金网数据爬取与深度分析

from requests_html import HTMLSession

from lxml import etree

import os

import xlwt

from xlutils.copy import copy

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.font_manager import FontProperties

session = HTMLSession()

class DataFetcher:

def __init__(self):

self.start_url = 'http://fund.eastmoney.com/fund.html'

self.next_url = 'http://fund.eastmoney.com/HBJJ_pjsyl.html'

self.user_agents = [

'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',

'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3451.0 Safari/537.36',

# More user agents omitted for brevity

]

def fetch_data(self):

headers = {'User-Agent': random.choice(self.user_agents)}

response = session.get(self.start_url, headers=headers).content

response = etree.HTML(response)

self.process_data(response)

def process_data(self, response):

name_list = response.xpath('//tbody/tr/td[5]/nobr/a[1]/text()')

num_1_list = response.xpath('//tbody/tr/td[6]/text()')

num_2_list = response.xpath('//tbody/tr/td[7]/text()')

self.save_to_excel(name_list, num_1_list, num_2_list)

self.analyze_data(name_list, num_1_list, num_2_list)

def save_to_excel(self, names, unit_values, accumulated_values):

path = os.getcwd() + '/data/'

if not os.path.exists(path):

os.mkdir(path)

excel_path = path + 'stock_data.xls'

if not os.path.exists(excel_path):

workbook = xlwt.Workbook(encoding='utf-8')

worksheet = workbook.add_sheet('Stock Data')

worksheet.write(0, 0, 'Stock Name')

worksheet.write(0, 1, 'Unit Value')

worksheet.write(0, 2, 'Accumulated Value')

workbook.save(excel_path)

else:

workbook = xlrd.open_workbook(excel_path)

sheets = workbook.sheet_names()

new_workbook = copy(workbook)

worksheet = new_workbook.get_sheet(0)

row = worksheet.nrows

for name, unit, accum in zip(names, unit_values, accumulated_values):

worksheet.write(row, 0, name)

worksheet.write(row, 1, unit)

worksheet.write(row, 2, accum)

new_workbook.save(excel_path)

def analyze_data(self, names, unit_values, accumulated_values):

plt.rcParams['font.sans-serif'] = ['SimHei']

plt.plot(names, accumulated_values, 'ro-', label='Accumulated Value')

plt.plot(names, unit_values, 'ro-', label='Unit Value')

plt.legend()

plt.xticks(rotation=270)

plt.xlabel('Stock Name')

plt.ylabel('Value')

plt.savefig('value_line_chart.png')

plt.show()

def run(self):

self.fetch_data()

if __name__ == '__main__':

fetcher = DataFetcher()

fetcher.run()

```