#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib.request
import urllib.error
import re
import threading
from lxml import etree
import time
import pandas as pd
import sys
import numpy as np #numpy计算包
import matplotlib.pyplot as plt
import chardet
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 5.0)
import matplotlib.pylab as pyl#折线图、散点图plot,直方图hist
import requests
from requests import get
headers = {'Accept':'*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Connection': 'keep-alive',
'Cookie': 'cna=um0tE7tQllkCAXE21MrqNDM2',
'DNT': 1,
'Host': 'afp.csbew.com',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0'}
url="http://www.jjwxc.net/topten.php?orderstr=7"
response = requests.get(url,params = headers)
response.encoding = "gb2312"#原网页为gb2312编码
html = response.text
html = etree.HTML(html)
bookname = html.xpath('//a[@class="tooltip"]/text()')
writenum = html.xpath('//td[@align="right"]/text()')
score = html.xpath('//tbody/tr/td[@bgcolor="#eefaee"]/text()')
df=pd.DataFrame(columns=["书名","字数","积分"])
for i in range(0,200):#共200条数据
book_name=bookname[i]
write_num=writenum[i].strip()#去掉空格占位符
score_num = score[i]
s = pd.Series({'书名':book_name,'字数':write_num, '积分':score_num})
df = df.append(s, ignore_index=True)#若不指定ignore_index参数,则会把添加的数据的index保留下来,若ignore_index=Ture则会对所有的行重新自动建立索引。
df.to_csv("D://jj2.csv",index=False,encoding="utf_8_sig")
data=pd.read_csv("D://jj2.csv")
data2=data.T
x1=data2.values[1]
y1=data2.values[2]
pyl.xlabel("write_num")#横轴
pyl.ylabel("score_num")#纵轴,不显示
pyl.ylim(800000000,6000000000)#不显示
pyl.ylim(0,100)
pyl.plot(x1,y1,'or')
pyl.show()
其实,三列数据可以直接从网站copy得到,此次只为做训练。下图纵轴不显示名称和坐标,待解决。
此图用spss做出,因纵坐标的不一致导致图形不同,经过分析,存在部分点为异常点,标准残差不在【-3,3】之间,可找出这些分析原因。