问一个人知乎账号想fo一下,结果她不告诉我,现在想想有点奇怪,有点好奇,好在我知道几个她点赞过的问题,想用社会工程学的方法筛选下,找出她的知乎账号。(匿了没法邀请,算了她应该不会来这个区)
<code class="language-python"><span class="c">#!/usr/bin/env python</span>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">from</span> <span class="nn">bs4</span> <span class="kn">import</span> <span class="n">BeautifulSoup</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="n">url</span> <span class="o">=</span> <span class="s">'http://www.zhihu.com'</span>
<span class="n">loginURL</span> <span class="o">=</span> <span class="s">'http://www.zhihu.com/login/email'</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">"User-Agent"</span><span class="p">:</span> <span class="s">'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0'</span><span class="p">,</span>
<span class="s">"Referer"</span><span class="p">:</span> <span class="s">"http://www.zhihu.com/"</span><span class="p">,</span>
<span class="s">'Host'</span><span class="p">:</span> <span class="s">'www.zhihu.com'</span><span class="p">,</span>
<span class="p">}</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">'email'</span><span class="p">:</span> <span class="s">'</span>
<span class="n">xxxxx</span><span class="nd">@gmail.com</span><span class="s">',</span>
<span class="s">'password'</span><span class="p">:</span> <span class="s">'</span>
<span class="n">xxxxxxx</span><span class="s">',</span>
<span class="s">'rememberme'</span><span class="p">:</span> <span class="s">"true"</span><span class="p">,</span>
<span class="p">}</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">session</span><span class="p">()</span>
<span class="c"># 如果成功登陆过,用保存的cookies登录</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">'cookiefile'</span><span class="p">):</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'cookiefile'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">cookie</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">cookies</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">cookie</span><span class="p">)</span>
<span class="n">req1</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'zhihu.html'</span><span class="p">,</span> <span class="s">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">req1</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
<span class="c"># 第一次需要手动输入验证码登录</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">req</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
<span class="k">print</span> <span class="n">req</span>
<span class="n">soup</span> <span class="o">=</span> <span class="n">BeautifulSoup</span><span class="p">(</span><span class="n">req</span><span class="o">.</span><span class="n">text</span><span class="p">,</span> <span class="s">"html.parser"</span><span class="p">)</span>
<span class="n">xsrf</span> <span class="o">=</span> <span class="n">soup</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">'input'</span><span class="p">,</span> <span class="p">{</span><span class="s">'name'</span><span class="p">:</span> <span class="s">'_xsrf'</span><span class="p">,</span> <span class="s">'type'</span><span class="p">:</span> <span class="s">'hidden'</span><span class="p">})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'value'</span><span class="p">)</span>
<span class="n">data</span><span class="p">[</span><span class="s">'_xsrf'</span><span class="p">]</span> <span class="o">=</span> <span class="n">xsrf</span>
<span class="n">timestamp</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">)</span>
<span class="n">captchaURL</span> <span class="o">=</span> <span class="s">'http://www.zhihu.com/captcha.gif?='</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">timestamp</span><span class="p">)</span>
<span class="k">print</span> <span class="n">captchaURL</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'zhihucaptcha.gif'</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">captchaREQ</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">captchaURL</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">captchaREQ</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
<span class="n">loginCaptcha</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">(</span><span class="s">'input captcha:</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">data</span><span class="p">[</span><span class="s">'captcha'</span><span class="p">]</span> <span class="o">=</span> <span class="n">loginCaptcha</span>
<span class="c"># print data</span>
<span class="n">loginREQ</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="n">loginURL</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">)</span>
<span class="c"># print loginREQ.url</span>
<span class="c"># print s.cookies.get_dict()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">loginREQ</span><span class="o">.</span><span class="n">json</span><span class="p">()[</span><span class="s">'r'</span><span class="p">]:</span>
<span class="c"># print loginREQ.json()</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'cookiefile'</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">cookies</span><span class="o">.</span><span class="n">get_dict</span><span class="p">(),</span> <span class="n">f</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span> <span class="s">'login failed, try again!'</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="c"># 以http://www.zhihu.com/question/27621722/answer/48820436这个大神的399各赞为例子.</span>
<span class="n">zanBaseURL</span> <span class="o">=</span> <span class="s">'http://www.zhihu.com/answer/22229844/voters_profile?&offset={0}'</span>
<span class="n">page</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
<span class="n">zanURL</span> <span class="o">=</span> <span class="n">zanBaseURL</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">page</span><span class="p">))</span>
<span class="n">page</span> <span class="o">+=</span> <span class="mi">10</span>
<span class="n">zanREQ</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">zanURL</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
<span class="n">zanData</span> <span class="o">=</span> <span class="n">zanREQ</span><span class="o">.</span><span class="n">json</span><span class="p">()[</span><span class="s">'payload'</span><span class="p">]</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">zanData</span><span class="p">:</span>
<span class="k">break</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">zanData</span><span class="p">:</span>
<span class="c"># print item</span>
<span class="n">zanSoup</span> <span class="o">=</span> <span class="n">BeautifulSoup</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s">"html.parser"</span><span class="p">)</span>
<span class="n">zanInfo</span> <span class="o">=</span> <span class="n">zanSoup</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s">'a'</span><span class="p">,</span> <span class="p">{</span><span class="s">'target'</span><span class="p">:</span> <span class="s">"_blank"</span><span class="p">,</span> <span class="s">'class'</span><span class="p">:</span> <span class="s">'zg-link'</span><span class="p">})</span>
<span class="k">if</span> <span class="n">zanInfo</span><span class="p">:</span>
<span class="k">print</span> <span class="s">'nickname:'</span><span class="p">,</span> <span class="n">zanInfo</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'title'</span><span class="p">),</span> <span class="s">' '</span><span class="p">,</span>
<span class="k">print</span> <span class="s">'person_url:'</span><span class="p">,</span> <span class="n">zanInfo</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'href'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">anonymous</span> <span class="o">=</span> <span class="n">zanSoup</span><span class="o">.</span><span class="n">find</span><span class="p">(</span>
<span class="s">'img'</span><span class="p">,</span> <span class="p">{</span><span class="s">'title'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="s">'class'</span><span class="p">:</span> <span class="s">"zm-item-img-avatar"</span><span class="p">})</span>
<span class="k">print</span> <span class="s">'nickname:'</span><span class="p">,</span> <span class="n">anonymous</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'title'</span><span class="p">)</span>
<span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">print</span> <span class="n">count</span>
</code>
这里有个 Python 3 的项目 Zhihu-py3 7sDream/zhihu-py3 · GitHub<code class="language-python"><span class="kn">from</span> <span class="nn">zhihu</span> <span class="kn">import</span> <span class="n">ZhihuClient</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">ZhihuClient</span><span class="p">(</span><span class="s">'cookies.json'</span><span class="p">)</span>
<span class="n">url</span> <span class="o">=</span> <span class="s">'http://www.zhihu.com/question/36338520/answer/67029821'</span>
<span class="n">answer</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">answer</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'问题:{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">answer</span><span class="o">.</span><span class="n">question</span><span class="o">.</span><span class="n">title</span><span class="p">))</span>
<span class="k">print</span><span class="p">(</span><span class="s">'答主:{0}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">answer</span><span class="o">.</span><span class="n">author</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
<span class="k">print</span><span class="p">(</span><span class="s">'此答案共有{0}人点赞:</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">answer</span><span class="o">.</span><span class="n">upvote_num</span><span class="p">))</span>
<span class="k">for</span> <span class="n">upvoter</span> <span class="ow">in</span> <span class="n">answer</span><span class="o">.</span><span class="n">upvoters</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">upvoter</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">upvoter</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
</code>
看到第一名的答案中的评论,补充一下如何发现aid这个关键特征的思路:<code class="language-cfm"><span class="nt"><div> <span class="na">data-copyable=</span><span class="s">"1"</span> <span class="na">data-isowner=</span><span class="s">"0"</span> <span class="na">data-helpful=</span><span class="s">"1"</span> <span class="na">data-deleted=</span><span class="s">"0"</span> <span class="na">data-created=</span><span class="s">"1444404675"</span> <span class="na">data-collapsed=</span><span class="s">"0"</span> <span class="na">data-atoken=</span><span class="s">"67029821"</span> <span class="na">data-author=</span><span class="s">"洛克"</span> <span class="na">data-qtoken=</span><span class="s">"36338520"</span> <span class="na">data-aid=</span><span class="s">"22229844"</span> <span class="na">itemtype=</span><span class="s">"http://schema.org/Answer"</span> <span class="na">itemscope=</span><span class="s">""</span> <span class="na">itemprop=</span><span class="s">"topAnswer"</span> <span class="na">class=</span><span class="s">"zm-item-answer"</span> <span class="na">tabindex=</span><span class="s">"-1"</span><span class="nt">></span>
我好奇的是,你说的社会工程学是啥?<br>据我所知,一般所谓社会工程学就是黑客的骗术,凭借已知信息骗取信任拿到自己要的信息,但是核心就是骗。<br>你现在是知道她点了哪个答案的赞,跟社会工程学有什么关系呢?<br>你是想说你知道她点过的多个答案,准备从同时赞过这些答案的人中找到她?<br>运气好可能一下子就找出来了,运气不好恐怕一堆候选人等着你。关键看你知道她赞过几个答案了。<br>论技术的话,我觉得用不着python写js在控制台跑就好了
找轮子哥 他有源码
轮子哥有爬取用户自动分析性别颜值值得关注程度的源码
</div></span></code>