人生苦短,我用Python--一起来爬知乎娘

2024-03-27 00:59

本文主要是介绍人生苦短,我用Python--一起来爬知乎娘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上次爬了教务处的成绩,接下来想去爬一爬知乎娘,好像大家都很喜欢爬知乎娘,GitHub上貌似已经有人把获取各种知乎数据的操作封装好了:zhihu-python ,但是良辰表示还是想自己试一试,我就爬点简单的,难度大的我也不会,我瞄准的是我自己关注的人的数据,差不多就这些东西:

关注者 / 提问 / 回答 / 赞同

那么下面开始分析,要拿到这个数据肯定是要先登陆的,那么先找到登陆界面:

登陆界面

这算是比较正常的界面,不知道为什么有的时候会出现新的验证码形式:

输入倒立的文字

这种形式登陆的话我看传递的表单数据是你鼠标点击的坐标,这种情况暂时不知道怎么办,留给高手,我们分析上面那种,知乎登陆分为手机号登陆或者邮箱登陆,先试试邮箱登陆,查看Post数据:

邮箱登陆

然后是手机号登陆的:

手机号登陆

现在理一下思路,我们要登录知乎要传递的数据如下:
- _xsrf() 用于防伪登陆
- password
- captcha 验证码
- phone_num/email 不同登陆方式传递的东西不同

phone_num/email以及passsword都需要自己输入,这个好办,我们需要解决如何获取 _xsrf和captcha 的问题,先解决 _xsrf,这个更简单一点,我们在知乎登陆页面 右键查看网页源代码,直接搜_xsrf:

<div class="view view-signin" data-za-module="SignInForm">
<form>
<input type="hidden" name="_xsrf" value="cf1ee28f15cea5dba3243a1c31a1b284"/>
<div class="group-inputs">

我们要做的就是解析出这个元素,直接上代码:

logn_url = 'http://www.zhihu.com/#signin'
session = requests.session()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36',
}
content = session.get(logn_url, headers=headers).content
soup = BeautifulSoup(content, 'html.parser')def getxsrf():return soup.find('input', attrs={'name': "_xsrf"})['value']

下面获取验证码, 我发现无论是使用哪一种登陆方式,Chrome的开发者面板都会有这么一行:

验证码请求

当我把这个Request URL输入浏览器中就会转到这个验证码图片的界面,说明这个验证码是我们加载这个页面时候,浏览器向服务器发出请求然后下载下来的,那么现在我们有了URL地址:

enter image description here

这还是一个GET请求,这里注意这个传递的r参数是会不断变化的,这是一个当前Unix的时间戳,知道了这些可以着手获取验证码然后让用户自己输入:

try:from PIL import Image
except:pass
# 获取验证码
def get_captcha():t = str(int(time.time() * 1000))captcha_url = 'http://www.zhihu.com/captcha.gif?r=' + t + "&type=login"r = session.get(captcha_url, headers=headers)with open('captcha.jpg', 'wb') as f:f.write(r.content)f.close()# 用pillow 的 Image 显示验证码# 如果没有安装 pillow 到源代码所在的目录去找到验证码然后手动输入try:im = Image.open('captcha.jpg')im.show()im.close()except:print(u'请到 %s 目录找到captcha.jpg 手动输入' % os.path.abspath('captcha.jpg'))captcha = input("please input the captcha\n>")return captcha

这里获取验证码的代码来自GitHub上的fuck-login项目,在此表示感谢,接下里就是写一个方法判断用户是使用了哪一种登陆方式,然后传递相应的数据:

def login(secret, account):# 通过输入的用户名判断是否是手机号if re.match(r"^1\d{10}$", account):print("手机号登录 \n")post_url = 'http://www.zhihu.com/login/phone_num'postdata = {'_xsrf': getxsrf(),'password': secret,'remember_me': 'true','phone_num': account,}else:print("邮箱登录 \n")post_url = 'http://www.zhihu.com/login/email'postdata = {'_xsrf': getxsrf(),'password': secret,'remember_me': 'true','email': account,}try:# 不需要验证码直接登录成功login_page = session.post(post_url, data=postdata, headers=headers)login_code = login_page.textprint(login_page.status)print(login_code)except:# 需要输入验证码后才能登录成功postdata["captcha"] = get_captcha()login_page = session.post(post_url, data=postdata, headers=headers)login_code = eval(login_page.text)print(login_code['msg'])# 这部分代码同样来自[**fuck-login**](https://github.com/xchaoinfo/fuck-login)项目,我偷了很多懒,囧

这个时候判断是否登陆成功可以随便查看一个页面发送GET请求看是否返回200,这里就不多说,接下来才是关键,为了获取我关注的人的信息,先打开开发者面板,这个地方打钩:

记得打钩

因为网页有时候会跳转,这里打钩之后新跳转的页面的信息就不会覆盖之前接受到的信息,然后找到我关注的人,URL地址是:

https://www.zhihu.com/people/GitSmile/followees

最开始我走了弯路,我以为直接在这个页面解析出我关注的人的信息就行,所以一开始我是这么做的,查看网页源代码,这里随便找一个我关注的人的信息:

<a title="死者代言人"
data-hovercard="p$t$forensic"
class="zm-item-link-avatar"
href="/people/forensic">
<img src="https://pic1.zhimg.com/be39d110759e68f389b7d2934d7353bc_m.jpg" class="zm-item-img-avatar">
</a>
<div class="zm-list-content-medium">
<h2 class="zm-list-content-title"><a data-hovercard="p$t$forensic" href="https://www.zhihu.com/people/forensic" class="zg-link author-link" title="死者代言人"
>死者代言人</a><span class="icon icon-badge-best_answerer icon-badge" data-tooltip="s$b$优秀回答者"></span></h2><div class="ellipsis">
<span class="badge-summary">优秀回答者</span>
<span class="bio">不养喵的爱喵法医。</span>
</div>
<div class="details zg-gray">
<a target="_blank" href="/people/forensic/followers" class="zg-link-gray-normal">35968 关注者</a>
/
<a target="_blank" href="/people/forensic/asks" class="zg-link-gray-normal">2 提问</a>
/
<a target="_blank" href="/people/forensic/answers" class="zg-link-gray-normal">305 回答</a>
/
<a target="_blank" href="/people/forensic" class="zg-link-gray-normal">51278 赞同</a>
</div>

然后在对比另一条:

<a title="陈亦飘"
data-hovercard="p$t$chen-yi-piao"
class="zm-item-link-avatar"
href="/people/chen-yi-piao">
<img src="https://pic2.zhimg.com/d495751efbf837b1b5b08571add7df3d_m.jpg" class="zm-item-img-avatar">
</a>
<div class="zm-list-content-medium">
<h2 class="zm-list-content-title"><a data-hovercard="p$t$chen-yi-piao" href="https://www.zhihu.com/people/chen-yi-piao" class="zg-link author-link" title="陈亦飘"
>陈亦飘</a></h2><div class="ellipsis"><span class="bio">音乐和电影是我的爱与慈悲</span>
</div>
<div class="details zg-gray">
<a target="_blank" href="/people/chen-yi-piao/followers" class="zg-link-gray-normal">74469 关注者</a>
/
<a target="_blank" href="/people/chen-yi-piao/asks" class="zg-link-gray-normal">0 提问</a>
/
<a target="_blank" href="/people/chen-yi-piao/answers" class="zg-link-gray-normal">80 回答</a>
/
<a target="_blank" href="/people/chen-yi-piao" class="zg-link-gray-normal">315971 赞同</a>
</div>

要分解出这些元素,只要找出他们的共同点,当时机智如我一眼就发现每一个我关注的人的名字都有这么一行元素:

class="zm-item-link-avatar"

而且都包含在一个<a></a>标签里面,这就好办了,使用BeautifulSoup先分解出每一个我关注的人的名字:

def getdetial():followees_url = 'https://www.zhihu.com/people/GitSmile/followees'followees_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Referer': 'https://www.zhihu.com/people/GitSmile/about','Upgrade-Insecure-Requests': '1','Accept-Encoding': 'gzip, deflate, sdch, br'}myfollowees = session.get(followees_url, headers=followees_headers)mysoup = BeautifulSoup(myfollowees.content, 'html.parser')print(mysoup.find('span', attrs={'class': 'zm-profile-section-name'}).text)

然后继续观察,突破点依然在这个<a></a>标签,拿”陈亦飘”的信息做个例子,看官们看这里面是不是有个href="/people/chen-yi-piao,然后看一下她的相关信息:

<a target="_blank" href="/people/chen-yi-piao/followers" class="zg-link-gray-normal">74469 关注者</a>
/
<a target="_blank" href="/people/chen-yi-piao/asks" class="zg-link-gray-normal">0 提问</a>
/
<a target="_blank" href="/people/chen-yi-piao/answers" class="zg-link-gray-normal">80 回答</a>
/
<a target="_blank" href="/people/chen-yi-piao" class="zg-link-gray-normal">315971 赞同</a>

发现没有,每一个信息里面的href元素都是我们最开始看到的/people/chen-yi-piao元素加上一些字符串组成的,那么我就可以在这上面做点文章:

获取详细信息
def getdetial():followees_url = 'https://www.zhihu.com/people/GitSmile/followees'followees_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Referer': 'https://www.zhihu.com/people/GitSmile/about','Upgrade-Insecure-Requests': '1','Accept-Encoding': 'gzip, deflate, sdch, br'}myfollowees = session.get(followees_url, headers=followees_headers)mysoup = BeautifulSoup(myfollowees.content, 'html.parser')print(mysoup.find('span', attrs={'class': 'zm-profile-section-name'}).text)for result in mysoup.findAll('a', attrs={'class': 'zm-item-link-avatar'}):print(result.get('title'))# 解析出href元素信息href = str(result.get('href'))# 关注者print(mysoup.find('a', attrs={'href': href + '/followers'}).text)# 提问print(mysoup.find('a', attrs={'href': href + '/asks'}).text)# 回答print(mysoup.find('a', attrs={'href': href + '/answers'}).text)# 赞同print(mysoup.find('a', attrs={'href': href, 'class': 'zg-link-gray-normal'}).text + '\n')

上面的代码应该很简单了,看一看就能看懂,然后就我信心满满地在PyCharm上输出的时候,发现来来回回只输出20条信息,也就是说,我关注了27人,但是控制台只输出了20个人的信息,然后我就上网查,发现不止一个人有我这样的疑问,当然也多亏了前人踩坑,网上给出来的答案是知乎获取关注的人的时候使用了AJAX技术,也就是动态加载,但是这一部分代码不会再网页Html代码中显示出来,所以为了获取其他关注的人的信息我这里要另辟蹊径。

然后我翻看我关注的人的信息的时候,在开发者面板抓到这么一条POST信息:

enter image description here

这条POST之后下面刷出来的图片是我关注的人的头像,并且这些头像在我之前看到的20条数据里面是没有的,加上我总共就关注了27个人,所以我有理由相信这个Post就是浏览器向服务器发送请求的Post,看一下Post的信息:

enter image description here

一个偏移量(offset),一个哈希值(hash_id)外加一个”order_by”:”created”的键值对,这里偏移量很好理解,这个”hash_id”据我多次登陆发现是一个不变的值,或者说每一台电脑或许精确一点每一个浏览器都会有这么一个给定的值,照抄,那么改进后的代码如下:

# 获取所有关注的人的信息
def getallview():nums = 27  # 这个是我关注的人数followees_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36','Referer': 'https://www.zhihu.com/people/GitSmile/followees','Origin': 'https://www.zhihu.com','Accept-Encoding': 'gzip, deflate, br','CG - Sid': '57226ad5 - 793b - 4a9d - 9791 - 2a9a17e682ef','Accept': '* / *'}# 序号count = 0for index in range(0, nums):fo_url = 'https://www.zhihu.com/node/ProfileFolloweesListV2'm_data = {'method': 'next','params': '{"offset":' + str(index) + ',"order_by":"created","hash_id":"de2cb64bc1afe59cf8a6e456ee5eaebc"}','_xsrf': str(getxsrf())}result = session.post(fo_url, data=m_data, headers=followees_headers)dic = json.loads(result.content.decode('utf-8'))li = dic['msg'][0]mysoup = BeautifulSoup(li, 'html.parser')for result in mysoup.findAll('a', attrs={'class': 'zm-item-link-avatar'}):print(index + 1)print(result.get('title'))href = str(result.get('href'))print(mysoup.find('a', attrs={'href': href + '/followers'}).text)print(mysoup.find('a', attrs={'href': href + '/asks'}).text)print(mysoup.find('a', attrs={'href': href + '/answers'}).text)print(mysoup.find('a', attrs={'href': href, 'class': 'zg-link-gray-normal'}).text + '\n')count += 1print('一共关注了 %d人' % count)

放上程序的入口:

if __name__ == '__main__':if isLogin():print('您已经登录')else:account = input('请输入你的用户名\n>  ')secret = input("请输入你的密码\n>  ")login(secret, account)getallview()

看一下实际效果:

enter image description here

写在结尾

虽然简单,但是提供了一些初学者的思路,下一次准备爬一些知乎上的图片,好像很多人都热衷于这种事,嘻嘻,荆轲刺秦王。

源码

这篇关于人生苦短,我用Python--一起来爬知乎娘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/850442

相关文章

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用