时间:2021-07-01 10:21:17 帮助过:21人阅读
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
import json
import os
import sys
url = '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 = 0
count = 0
while 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 · GitHubfrom zhihu import ZhihuClient
client = 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这个关键特征的思路: 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在控制台跑就好了
找轮子哥 他有源码
轮子哥有爬取用户自动分析性别颜值值得关注程度的源码