2014年1月12日 星期日

隔了一年的更新:以 python 實現 pixiv 的一些分析

在網路上看到一個分析 pixiv 小說中ラブライブ配對(カップリング)的數量
好像主要是用 R 語言實現的
http://d.hatena.ne.jp/MikuHatsune/20130905/1378299747
於是也試著用 python 作一次
先放結論:我搜尋了4頁 tag (タッグ) 中含有ラブライブ的
得到以下分佈


  | こと うみ  ほの りん  ぱな まき  のぞ えり  にこ
こと|   0    4    0    0    1    0    0    0    0
うみ|   0    0    1    0    0    0    0    0    0
ほの|   0    0    0    0    0    1    0    0    0
りん|   0    0    0    0    3    2    0    0    0
ぱな|   0    0    0    0    0    0    0    0    0
まき|   0    0    0    0    0    0    0    0   16
のぞ|   0    0    0    0    0    0    0    3    1
えり|   0    0    0    0    0    0    0    0    0
にこ|   0    0    0    0    0    0    0    0    0

裡面有一句
 "にこまきの圧倒的多さ。"
 根本次實驗的結論相同



實做

# -*- coding: utf-8 -*-
import httplib, csv, numpy
from StringIO import StringIO
from urllib import quote_plus, quote
from sys import stdout
#import matplotlib

students = ["こと", "うみ", "ほの", "りん", "ぱな", "まき", "のぞ", "えり", "にこ"]
qString = '/iphone/search.php?s_mode=s_tag&p={0}&word=' + quote_plus('ラブライブ')

def match_pairs(tags):
    stuCnt = 0 
    for tag in tags:
        findPair = [ind for (ind, student) in enumerate(students) if tag.find(student)!=-1]
        if len(findPair) == 2:
            return tuple(findPair)
    return None

pairStat = numpy.zeros((len(students), len(students)), dtype=numpy.uint)
for i in xrange(1, 5): 
    conn = httplib.HTTPConnection("spapi.pixiv.net")
    conn.request('POST', qString.format(i))
    res = conn.getresponse()
    cr = csv.reader(StringIO(res.read()))
    conn.close()
    for row in cr:
        tags = row[13].split()
        pair = match_pairs(tags)
        if (pair):
            pairStat[pair] += 1

def print10ele(head, eles):
    print '{:>4}|{:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4}'.format(head, *eles)

print10ele('', students)
for i, student in enumerate(students):
    print10ele(student, list(pairStat[i]))
pastie.org/735195# 這個網站有提供 pixiv 給 smart phone API 的簡單說明
對我們應用好像夠了,本來想說要登入的
因此最先是使用 httplib 作,不過在不用維護 session 的情形下是不是 urllib 比較方便啊?

code註解
先初始化一個矩陣


pairStat = numpy.zeros((len(students), len(students)), dtype=numpy.uint)
對前4頁


for i in xrange(1, 5): 
    conn = httplib.HTTPConnection("spapi.pixiv.net")
    conn.request('POST', qString.format(i))
    res = conn.getresponse()
pixiv 給的格式好像是 csv,所以就用內建的 csv parser 啦
第 14 column 好像是 tag 列表,用空白鍵分開

cr = csv.reader(StringIO(res.read()))
    conn.close()
    for row in cr:
        tags = row[13].split()
        pair = match_pairs(tags)
        if (pair):
            pairStat[pair] += 1
找找有幾個 μ's 成員在 tag 裡面,兩個就當作這個 tag 是配對

def match_pairs(tags):
    stuCnt = 0 
    for tag in tags:
        findPair = [ind for (ind, student) in enumerate(students) if tag.find(student)!=-1]
        if len(findPair) == 2:
            return tuple(findPair)
    return None
輸出結果

def print10ele(head, eles):
    print '{:>4}|{:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4}'.format(head, *eles)

print10ele('', students)
for i, student in enumerate(students):
    print10ele(student, list(pairStat[i]))
大體來說缺陷有這些
(1) 因為懶的等抓資料所以就只分析了 4 頁
(2) 不知道怎麼畫圖所以就沒有畫像 reference 那樣精美的圖,如果有的話會畫的(看那精美的 import matplotlib 被註解掉了)

沒有留言:

張貼留言