Python中怎么分析網(wǎng)站日志數(shù)據(jù)

Python中怎么分析網(wǎng)站日志數(shù)據(jù),針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

在武夷山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站制作,武夷山網(wǎng)站建設(shè)費(fèi)用合理。

數(shù)據(jù)來源

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import apache_log_parser        # 首先通過 pip install apache_log_parser 安裝庫
%matplotlib inline
fformat = '%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %T'  # 創(chuàng)建解析器
p = apache_log_parser.make_parser(fformat)
sample_string = 'koldunov.net 85.26.235.202 - - [16/Mar/2013:00:19:43 +0400] "GET /?p=364 HTTP/1.0" 200 65237 "http://koldunov.net/?p=364" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11" 0'
data = p(sample_string) #解析后的數(shù)據(jù)為字典結(jié)構(gòu)
data

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

datas = open(r'H:\python數(shù)據(jù)分析\數(shù)據(jù)\apache_access_log').readlines()  #逐行讀取log數(shù)據(jù)
log_list = []  # 逐行讀取并解析為字典
for line in datas:
data = p(line)
data['time_received'] = data['time_received'][1:12]+' '+data['time_received'][13:21]+' '+data['time_received'][22:27] #時(shí)間數(shù)據(jù)整理
log_list.append(data)    #傳入列表
log = pd.DataFrame(log_list)   #構(gòu)造DataFrame
log = log[['status','response_bytes_clf','remote_host','request_first_line','time_received']]   #提取感興趣的字段
log.head()
#status 狀態(tài)碼 response_bytes_clf 返回的字節(jié)數(shù)(流量)remote_host 遠(yuǎn)端主機(jī)IP地址 request_first_line 請求內(nèi)容t ime_received 時(shí)間數(shù)據(jù)

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

log['time_received'] = pd.to_datetime(log['time_received']) #把time_received字段轉(zhuǎn)換為時(shí)間數(shù)據(jù)類型,并設(shè)置為索引
log = log.set_index('time_received')
log.head()

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

log['status'] = log['status'].astype('int') # 轉(zhuǎn)換為int類型
log['response_bytes_clf'].unique()
array(['26126', '10532', '1853', ..., '66386', '47413', '48212'], dtype=object)
log[log['response_bytes_clf'] == '-'].head() #對response_bytes_clf字段進(jìn)行轉(zhuǎn)換時(shí)報(bào)錯(cuò),查找原因發(fā)現(xiàn)其中含有“-”

def dash3nan(x):    # 定義轉(zhuǎn)換函數(shù),當(dāng)為“-”字符時(shí),將其替換為空格,并將字節(jié)數(shù)據(jù)轉(zhuǎn)化為M數(shù)據(jù)
   if x == '-':
x = np.nan
   else:
x = float(x)/1048576
   return x
log['response_bytes_clf'] = log['response_bytes_clf'].map(dash3nan)
log.head()

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

log.dtypes

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

流量起伏不大,但有一個(gè)極大的峰值超過了20MB。

log[log['response_bytes_clf']>20] #查看流量峰值

t_log = log['response_bytes_clf'].resample('30t').count()
t_log.plot()

對時(shí)間重采樣(30min),并計(jì)數(shù) ,可看出每個(gè)時(shí)間段訪問的次數(shù),早上8點(diǎn)訪問次數(shù)最多,其余時(shí)間處于上下波動中。

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

h_log = log['response_bytes_clf'].resample('H').count()
h_log.plot()

當(dāng)繼續(xù)轉(zhuǎn)換頻率到低頻率時(shí),上下波動不明顯。

d_log = pd.DataFrame({'count':log['response_bytes_clf'].resample('10t').count(),'sum':log['response_bytes_clf'].resample('10t').sum()})    
d_log.head()

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

構(gòu)造訪問次數(shù)和訪問流量的 DataFrame。

plt.figure(figsize=(10,6))   #設(shè)置圖表大小
ax1 = plt.subplot(111)    #一個(gè)subplot
ax2 = ax1.twinx()     #公用x軸
ax1.plot(d_log['count'],color='r',label='count')
ax1.legend(loc=2)
ax2.plot(d_log['sum'],label='sum')
ax2.legend(loc=0)

繪制折線圖,有圖可看出,訪問次數(shù)與訪問流量存在相關(guān)性。

IP地址分析

ip_count = log['remote_host'].value_counts()[0:10] #對remote_host計(jì)數(shù),并取前10位
ip_count

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

ip_count.plot(kind='barh') #IP前十位柱狀圖

import pygeoip # pip install pygeoip 安裝庫
# 同時(shí)需要在網(wǎng)站上(http://dev.maxmind.com/geoip/legacy/geolite)下載DAT文件才能解析IP地址
gi = pygeoip.GeoIP(r'H:\python數(shù)據(jù)分析\數(shù)據(jù)\GeoLiteCity.dat', pygeoip.MEMORY_CACHE)
info = gi.record_by_addr('64.233.161.99')
info #解析IP地址

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

ips = log.groupby('remote_host')['status'].agg(['count']) # 對IP地址分組統(tǒng)計(jì)
ips.head()

ips.drop('91.224.246.183',inplace=True)

ips['country'] = [gi.record_by_addr(i)['country_code3'] for i in ips.index] # 將IP解析的國家和經(jīng)緯度寫入DataFrame
ips['latitude'] = [gi.record_by_addr(i)['latitude'] for i in ips.index]
ips['longitude'] = [gi.record_by_addr(i)['longitude'] for i in ips.index]

ips.head()
country = ips.groupby('country')['count'].sum() #對country字段分組統(tǒng)計(jì)
country = country.sort_values(ascending=False)[0:10] # 篩選出前10位的國家
country

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

country.plot(kind='bar')

俄羅斯的訪問量最多,可推斷該網(wǎng)站來源于俄羅斯。

from mpl_toolkits.basemap import Basemap

plt.style.use('ggplot')
plt.figure(figsize=(10,6))

map1 = Basemap(projection='robin', lat_0=39.9, lon_0=116.3,
resolution = 'l', area_thresh = 1000.0)

map1.drawcoastlines()
map1.drawcountries()
map1.drawmapboundary()

map1.drawmeridians(np.arange(0, 360, 30))
map1.drawparallels(np.arange(-90, 90, 30))

size = 0.03
for lon, lat, mag in zip(list(ips['longitude']), list(ips['latitude']), list(ips['count'])):
x,y = map1(lon, lat)
msize = mag * size
map1.plot(x, y, 'ro', markersize=msize)

Python中怎么分析網(wǎng)站日志數(shù)據(jù)

關(guān)于Python中怎么分析網(wǎng)站日志數(shù)據(jù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

本文名稱:Python中怎么分析網(wǎng)站日志數(shù)據(jù)
文章地址:http://muchs.cn/article38/johcsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站改版電子商務(wù)、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)