如何找出知乎的所有神回复
2014-09-01 16:27
246 查看
有时候看到神回复,感觉真是惊叹!先上几个看看
神回复总是言简意赅,吐槽精准,回答到位,甚至是让人忍俊不禁,如何找到知乎的所有神回复,有人说找那些投票多并且回答字数少的answer,对于一个计算机的同学,不可能就是这样一条条去翻一吧,让我们写一个爬虫,抓下知乎的问题,每个问题保留投票最高的回答。首先需要一个得到问题列表,这里列表可以在http://www.zhihu.com/log/questions找到,爬问题列表的代码如下:
得到问题列表后再抓取每个问题投票最高的回复,代码如下:
现在我们得到了每个问题的标题、投票最高的回复、问题链接。接下来我们需要把“回复短投票高”这样的规则进行量化计算,很明显一个回复是神回复的可能性与投票数成正比,与回复文本的长度成反比,但实现上,我们需要注意一些细节,比如有些神配图,一言不语,文本长度为0,所以需要平滑一些,另外回复越段应该越精辟,于是我定义了如下公式:$$Score=\frac{vote}{5+\frac{answer\_len^2}{10}}$$
这个公式整体来说,认为是神回复的可能与投票数成正比,与回复的长度的平方成反比,加5是为了平滑哪些神配图。
爬了一个晚上,爬取了2万个问题,然后按Score计算,取Score最大的top 1000,欣赏神回复,请移步至github。
问题爬得太少,更多的精辟神回复没有找出来。
转载请注明出处:http://www.cnblogs.com/fengfenggirl/
神回复总是言简意赅,吐槽精准,回答到位,甚至是让人忍俊不禁,如何找到知乎的所有神回复,有人说找那些投票多并且回答字数少的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/
相关文章推荐
- 不显示删除回复显示所有回复显示星级回复显示得分回复 如何将工具栏按钮和菜单栏的选项关联起来执行同一个动作啊
- 不显示删除回复显示所有回复显示星级回复显示得分回复 如何简便地将一台机器上的oracle数据库迁移到另一台机器上[问题点数:100分]
- 如何在二叉树中找出和为某一值的所有路径
- 在OllyDbg中如何找出B模块中所有调用了A模块的C方法的地方
- 如何在 Linux shell 中找出所有包含指定文本的文件
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- 李开复在知乎上关于如何有效利用时间的回复
- 反驳--(知乎)大龄门外汉如何进入软件开发行业?的回复
- 反驳--(知乎)大龄门外汉如何进入软件开发行业?的回复
- 如何利用已有的cook模拟登陆知乎,并爬取相关问题下所有的答案
- 如何找出标有"App Store 精华","Essentials"的所有软件?
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 如何阻止多次弹框
- 不显示删除回复显示所有回复显示星级回复显示得分回复 触发器问题:如何记录被修改的多行纪录的关键字的原值和新值?
- 如何找出所有memcached里的所有存放的元素
- torch中如何找出矩阵中元素之大于某个阈值的所有元素的下标?torch.find() ?
- 老男孩教育每日一题-第102天-如何找出/usr/local 下面所有shell脚本文件,并设置执行权限?
- 如何找出所有产品中的缺失图片?
- 如何找出标有"App Store 精华","Essentials"的所有软件?
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 如何绘制类似于Windows开始菜单的菜单
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 dev 控件中 gridControl1 控件的button列 事件中如何获取所在行的某列的值