如何寫個爬蟲程式扒下知乎某個回答所有點贊使用者名稱單?

來源:互聯網
上載者:User
問一個人知乎帳號想fo一下,結果她不告訴我,現在想想有點奇怪,有點好奇,好在我知道幾個她點贊過的問題,想用社交工程學的方法篩選下,找出她的知乎帳號。(匿了沒法邀請,算了她應該不會來這個區)

回複內容:

每個回答的div裡面都有一個叫 data-aid="12345678"的東西,
然後根據, www.zhihu.com/answer/12345678/voters_profile?&offset=10
這個json資料連線分析所有點贊的id和個人串連就行, 每10的點贊人數為一個json串連.

剛剛試了一下,需要登陸之後才能得到完整的資料, 登陸知乎可以參考我寫的部落格.
python類比登陸知乎


比如我這個回答的data-aid = '22229844'

#!/usr/bin/env python# -*- coding: utf-8 -*-import requestsfrom bs4 import BeautifulSoupimport timeimport jsonimport osimport sysurl = 'http://www.zhihu.com'loginURL = 'http://www.zhihu.com/login/email'headers = {    "User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0',    "Referer": "http://www.zhihu.com/",    'Host': 'www.zhihu.com',}data = {    'email': 'xxxxx@gmail.com',    'password': 'xxxxxxx',    'rememberme': "true",}s = requests.session()# 如果成功登陸過,用儲存的cookies登入if os.path.exists('cookiefile'):    with open('cookiefile') as f:        cookie = json.load(f)    s.cookies.update(cookie)    req1 = s.get(url, headers=headers)    with open('zhihu.html', 'w') as f:        f.write(req1.content)# 第一次需要手動輸入驗證碼登入else:    req = s.get(url, headers=headers)    print req    soup = BeautifulSoup(req.text, "html.parser")    xsrf = soup.find('input', {'name': '_xsrf', 'type': 'hidden'}).get('value')    data['_xsrf'] = xsrf    timestamp = int(time.time() * 1000)    captchaURL = 'http://www.zhihu.com/captcha.gif?=' + str(timestamp)    print captchaURL    with open('zhihucaptcha.gif', 'wb') as f:        captchaREQ = s.get(captchaURL)        f.write(captchaREQ.content)    loginCaptcha = raw_input('input captcha:\n').strip()    data['captcha'] = loginCaptcha    # print data    loginREQ = s.post(loginURL,  headers=headers, data=data)    # print loginREQ.url    # print s.cookies.get_dict()    if not loginREQ.json()['r']:        # print loginREQ.json()        with open('cookiefile', 'wb') as f:            json.dump(s.cookies.get_dict(), f)    else:        print 'login failed, try again!'        sys.exit(1)# 以http://www.zhihu.com/question/27621722/answer/48820436這個大神的399各贊為例子.zanBaseURL = 'http://www.zhihu.com/answer/22229844/voters_profile?&offset={0}'page = 0count = 0while 1:    zanURL = zanBaseURL.format(str(page))    page += 10    zanREQ = s.get(zanURL, headers=headers)    zanData = zanREQ.json()['payload']    if not zanData:        break    for item in zanData:        # print item        zanSoup = BeautifulSoup(item, "html.parser")        zanInfo = zanSoup.find('a', {'target': "_blank", 'class': 'zg-link'})        if zanInfo:            print 'nickname:', zanInfo.get('title'),  '    ',            print 'person_url:', zanInfo.get('href')        else:            anonymous = zanSoup.find(                'img', {'title': True, 'class': "zm-item-img-avatar"})            print 'nickname:', anonymous.get('title')        count += 1    print count
這裡有個 Python 3 的項目 Zhihu-py3 7sDream/zhihu-py3 · GitHub

封裝了知乎爬蟲的各方面需求,比如擷取使用者資訊,擷取問題資訊,擷取答案資訊,之類的……當然也包括點贊使用者啥的,雖然是單線程同步式 但是平常用用還是可以滴。

這裡是她的文檔:Welcome to zhihu-py3’s documentation!

歡迎 Star 以及 Fork 或者貢獻代碼。

===

擷取點贊使用者灰常簡單 大概就這樣

from zhihu import ZhihuClientclient = ZhihuClient('cookies.json')url = 'http://www.zhihu.com/question/36338520/answer/67029821'answer = client.answer(url)print('問題:{0}'.format(answer.question.title))print('答主:{0}'.format(answer.author.name))print('此答案共有{0}人點贊:\n'.format(answer.upvote_num))for upvoter in answer.upvoters:    print(upvoter.name, upvoter.url)
看到第一名的答案中的評論,補充一下如何發現aid這個關鍵特徵的思路:

一句話概述:對人工操作時發送的HTTP Request/Response進行分析,找出關鍵定位特徵。
工具:firebug

1. 點擊 任意一個答案頁面下面的超連結 等人贊同 發現會向類似於這樣的
http://www.zhihu.com/answer/22229844/voters_profile
URL發送資料。
從這個URL的格式上已經很容易猜到這就是給答案22229844 進行投票的投票者資料了,一看伺服器返回的Response (一段JSON資料)也能說明這一點。那麼只要我們可以向這個URL發送一段GET請求就能知道投票者了。剩下的就是要解決怎麼找出這個URL的問題,也就是找到這個22229844 。
2. 既然知道當點擊 等人贊同 會觸發一段Ajax向這個URL發送請求,那這個22229844 要麼在DOM中儲存了,要麼是計算出來的。既然如此,在DOM中搜尋22229844這個字串,很輕鬆就能找到這樣的一個div:
 data-copyable="1" data-isowner="0" data-helpful="1" data-deleted="0" data-created="1444404675" data-collapsed="0" data-atoken="67029821" data-author="洛克" data-qtoken="36338520" data-aid="22229844" itemtype="http://schema.org/Answer" itemscope="" itemprop="topAnswer" class="zm-item-answer" tabindex="-1">
我好奇的是,你說的社交工程學是啥?
據我所知,一般所謂社交工程學就是駭客的騙術,憑藉已知資訊騙取信任拿到自己要的資訊,但是核心就是騙。
你現在是知道她點了哪個答案的贊,跟社交工程學有什麼關係呢?
你是想說你知道她點過的多個答案,準備從同時贊過這些答案的人中找到她?
運氣好可能一下子就找出來了,運氣不好恐怕一堆候選人等著你。關鍵看你知道她贊過幾個答案了。
論技術的話,我覺得用不著python寫js在控制台跑就好了找輪子哥 他有源碼輪子哥有爬取使用者自動分析性別顏值值得關注程度的源碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.