如何找出知乎的所有神回復


  有時候看到神回復,感覺真是驚嘆!先上幾個看看

你小時候父母為了讓你能夠努力學習,都用過什么喪盡天良的方法? - 知乎
給了我這張臉

求職者在答應企業面試后,沒有任何說明而爽約是否妥當? - 知乎
你們企業也經常說“回去等我們的電話吧”然后沒有任何下文啊。

什么叫見過大世面 ? -知乎

能享受最好的,能承受最壞的

  神回復總是言簡意賅,吐槽精准,回答到位,甚至是讓人忍俊不禁,如何找到知乎的所有神回復,有人說找那些投票多並且回答字數少的answer,對於一個計算機的同學,不可能就是這樣一條條去翻一吧,讓我們寫一個爬蟲,抓下知乎的問題,每個問題保留投票最高的回答。首先需要一個得到問題列表,這里列表可以在http://www.zhihu.com/log/questions找到,爬問題列表的代碼如下:  

def getQuestions(start,offset='20'):
    #cookies = urllib2.HTTPCookieProcessor()
    #opener = urllib2.build_opener(cookies)
    #urllib2.install_opener(opener)

    header = {"Accept":"*/*",
    "Accept-Encoding":"gbk,utf-8,gzip,deflate,sdch",
    "Accept-Language":"zh-CN,zh;q=0.8,en;q=0.6",
    "Connection":"keep-alive",
    "Content-Length":"64",
    "Content-Type":"application/x-www-form-urlencoded; charset=utf-8",
    'Cookie':'*************'
    "Host":"www.zhihu.com",
    "Origin":"http://www.zhihu.com",
    "Referer":"http://www.zhihu.com/log/questions",
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36",
    "X-Requested-With":"XMLHttpRequest"
    }

    parms = {'start':start,
            'offset':offset,
            '_xsrf':'*************'}
    url = 'http://www.zhihu.com/log/questions'
    req = urllib2.Request(url,headers=header,data=urllib.urlencode(parms))
    content = urllib2.urlopen( req ).read()
    html = gzip.GzipFile(fileobj = cStringIO.StringIO(content)).read()
    html = eval(html)['msg'][1]
    pageSoup = BeautifulSoup(html)
    questions = []
    items = pageSoup.find_all('div',{'class':'zm-item'})
    for item in items:
        url = item.find_all('a',{'target':'_blank'})[0].get('href').rsplit('/',1)[1]
        questions.append(url)
    lastId = items[-1].get('id').split('-')[1]
    return questions,lastId

  得到問題列表后再抓取每個問題投票最高的回復,代碼如下:

def getArticle(url):
    page = getPage(url)
    pageSoup = BeautifulSoup(page)
    title = str(pageSoup.title).replace('<title>','').replace('</title>','').strip()
    item = pageSoup.find_all('div',{'class':'zm-item-answer'})
    if item is None or len(item) == 0:
        return None
    anwser = item[0].find('div',{'class':'fixed-summary zm-editable-content clearfix'}).get_text().strip()
    vote = item[0].find('div',{'class':'zm-item-vote-info '}).get('data-votecount').strip()
    anwser = formatStr(anwser)
    ans_len = len(anwser)
    if ans_len > 100:
        anwser = anwser[0:100]
    title = formatStr(title)
    out = [title, anwser, str(ans_len),vote,url]
    return out

  現在我們得到了每個問題的標題、投票最高的回復、問題鏈接。接下來我們需要把“回復短投票高”這樣的規則進行量化計算,很明顯一個回復是神回復的可能性與投票數成正比,與回復文本的長度成反比,但實現上,我們需要注意一些細節,比如有些神配圖,一言不語,文本長度為0,所以需要平滑一些,另外回復越段應該越精辟,於是我定義了如下公式:$$Score=\frac{vote}{5+\frac{answer\_len^2}{10}}$$

  這個公式整體來說,認為是神回復的可能與投票數成正比,與回復的長度的平方成反比,加5是為了平滑哪些神配圖。

  爬了一個晚上,爬取了2萬個問題,然后按Score計算,取Score最大的top 1000,欣賞神回復,請移步至github

  問題爬得太少,更多的精辟神回復沒有找出來。

轉載請注明出處:http://www.cnblogs.com/fengfenggirl/   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM