# Zhihu-Spider
一个获取知乎用户主页信息的多线程Python爬虫程序。
简介:
* 使用[Requests](http://www.python-requests.org/en/master/)模拟HTTP请求/响应,[Beautiful Soup 4](https://www.crummy.com/software/BeautifulSoup/)提取页面信息。
* 使用Python内置的Thread多线程和IP代理提升爬取速度,并绕过知乎的反爬虫机制。
* 使用Python内置的query作为消息队列。
* 用csv文件存储数据。
## 环境依赖
* beautifulsoup4
* requests
## 使用方法
在项目路径下输入以安装需要的模块:
```shell
$ pip install -r requirments.txt
```
打开proxy.py文件,在""处填写代理隧道验证信息:
```python
# 代理隧道验证信息
proxyUser = ""
proxyPass = ""
```
(笔者使用一个IP代理隧道,若需要使用普通IP代理,则需要改写getproxies函数,关于代理的细节在后文中详述。)
运行:
```shell
$ python spider/run.py
```
## 数据
运行爬虫一段时间后,将会在项目目录下的datafile文件夹中找到存储数据的csv文件。
![](./image/datafilelist.png)
每个csv文件100MB,以方便数据分析,同时降低文件意外损坏带来的损失。
数据格式为典型的表格:
![](./image/datafile.png)
第一行为表头,表格一共三列:
1. **第一列为用户的url token**,即用户主页url中的id,如用户vczh的主页链接:https://www.zhihu.com/people/excited-vczh ,那么vczh的token就是excited-vczh,这个token具有唯一性。
2. **第二列为对应用户的数据**,数据是json格式的,因为知乎在页面中是以json来传送用户信息的,所以就直接存储了这个json,Python内置json读写函数,非常方便数据分析。
以下是整理格式后,用户vczh的数据:
```json
{
"isFollowed":false,
"educations":[
{
"major":{
"url":"http://www.zhihu.com/api/v4/topics/19590324",
"avatarUrl":"https://pic1.zhimg.com/e82bab09c_is.jpg",
"name":"软件学院",
"introduction":"",
"type":"topic",
"excerpt":"",
"id":"19590324"
},
"school":{
"url":"http://www.zhihu.com/api/v4/topics/19599737",
"avatarUrl":"https://pic2.zhimg.com/4d0d193a9_is.jpg",
"name":"华南理工大学(SCUT)",
"introduction":"华南理工大学(South China University of Technology)(原华南工学院,1952年建立):教育部直属的重点大学,涵盖理、工、管、经、文、法等多学科,先后成为“211工程”和“985工程”院校,被誉为中国“南方工科大学的一面旗帜”,“工程师的摇篮”,“企业家的摇篮”。校园分为两个校区,北校区位于广州市天河区五山高校区,南校区位于广州市番禺区广州大学城内。学校占地面积4417亩(其中南校区1677亩)。北校区湖光山色交相辉映,绿树繁花香飘四季,民族式建筑与现代化楼群错落有致,环境优美清新,文化底蕴深厚,是教育部命名的“文明校园”;南校区是一个环境优美、设施先进、管理完善、制度创新的现代化校园,是莘莘学子求学的理想之地。",
"type":"topic",
"excerpt":"华南理工大学(South China University of Technology)(原华南工学院,1952年建立):教育部直属的重点大学,涵盖理、工、管、经、文、法等多学科,先后成为“211工程”和“985工程”院校,被誉为中国“南方工科大学的一面旗帜”,“工程师的摇篮”,“企业家的摇篮”。校园分为两个校区,北校区位于广州市天河区五山高校区,南校区位于广州市番禺区广州大学城内。学校占地面积4417亩(其中南校区1677亩)。北校区湖光山色交相辉…",
"id":"19599737"
}
}
],
"followingCount":2263,
"voteFromCount":0,
"userType":"people",
"showSinaWeibo":false,
"pinsCount":0,
"isFollowing":false,
"markedAnswersText":"编辑推荐",
"isPrivacyProtected":false,
"accountStatus":[
],
"isForceRenamed":false,
"id":"0970f947b898ecc0ec035f9126dd4e08",
"favoriteCount":1,
"voteupCount":1388515,
"commercialQuestionCount":0,
"isBlocking":false,
"followingColumnsCount":73,
"headline":"专业造轮子,拉黑抢前排。gaclib.net",
"urlToken":"excited-vczh",
"participatedLiveCount":6,
"followingFavlistsCount":20,
"isAdvertiser":false,
"isBindSina":true,
"favoritedCount":236566,
"isOrg":false,
"followerCount":583782,
"employments":[
{
"company":{
"url":"http://www.zhihu.com/api/v4/topics/19557307",
"avatarUrl":"https://pic3.zhimg.com/v2-d3a9ee5ba3a2fe711087787c6169dcca_is.jpg",
"name":"Microsoft Office",
"introduction":"Microsoft Office 是一套由微软开发的办公软件。",
"type":"topic",
"excerpt":"Microsoft Office 是一套由微软开发的办公软件。",
"id":"19557307"
},
"job":{
"url":"http://www.zhihu.com/api/v4/topics/19578588",
"avatarUrl":"https://pic1.zhimg.com/e82bab09c_is.jpg",
"name":"Developer",
"introduction":"",
"type":"topic",
"excerpt":"",
"id":"19578588"
}
}
],
"type":"people",
"avatarHue":"",
"avatarUrlTemplate":"https://pic1.zhimg.com/3a6c25ac3864540e80cdef9bc2a73900_{size}.jpg",
"followingTopicCount":34,
"description":"长期开发跨三大PC平台的GUI库<br><a href="https://link.zhihu.com/?target=http%3A//www.gaclib.net" class=" external" target="_blank" rel="nofollow noreferrer"><span class="invisible">http://www.</span><span class="visible">gaclib.net</span><span class="invisible"></span><i class="icon-external"></i></a>,讨论QQ群:231200072(不闲聊)<br>不再更新的技术博客:<a href="https://link.zhihu.com/?target=http%3A//www.cppblog.com/vczh" class=" external" target="_blank" rel="nofollow noreferrer"><span class="invisible">http://www.</span><span class="visible">cppblog.com/vczh</span><span class="invisible"></span><i class="icon-external"></i></a>",
"business":{
"url":"http://www.zhihu.com/api/v4/topics/19619368",
"avatarUrl":"https://pic1.zhimg.com/e82bab09c_is.jpg",
"name":"计算机软件",
"introduction":"徼",
"type":"topic",
"excerpt":"徼",
"id":"19619368"
},
"avatarUrl":"https://pic1.zhimg.com/3a6c25ac3864540e80cdef9bc2a73900_is.jpg",
"columnsCount":5,
"hostedLiveCount":0,
"isActive":1,
"thankToCount":0,
"mutualFolloweesCount":0,
"markedAnswersCount":4,
"coverUrl":"https://pic1.zhimg.com/v2-67b965aa94a92ed49b1a4205145b5cf4_b.jpg",
"thankFromCount":0,
"voteToCount":0,
"isBlocked":false,
"answerCount":16163,
"allowMessage":false,
"articlesCount":66,
"name":"vczh",
"questionCount":487,
"locations":[
{
"url":"http://www.zhihu.com/api/v4/topics/19583552",
"avatarUrl":"https://pic4.zhimg.com/161f6ece791a4950ded3278fb74a2f9b_is.jpg",
"name":"西雅图(Seattle)",
"introduction":"西雅图是美国西北部最大的城市。多家高科技公司的总部(Microsoft, Amazon,Boeing 等等)�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
爬虫python入门 个获取知乎用户主页信息的多线程Python爬虫程序。 简介: * 使用[Requests](http://www.python-requests.org/en/master/)模拟HTTP请求/响应,[Beautiful Soup 4](https://www.crummy.com/software/BeautifulSoup/)提取页面信息。 * 使用Python内置的Thread多线程和IP代理提升爬取速度,并绕过知乎的反爬虫机制。 * 使用Python内置的query作为消息队列。 * 用csv文件存储数据。 ## 环境依赖 * beautifulsoup4 * requests ## 使用方法 在项目路径下输入以安装需要的模块: ```shell $ pip install -r requirments.txt ``` 打开proxy.py文件,在""处填写代理隧道验证信息: ```python # 代理隧道验证信息 proxyUser = "" proxyPass = "" ``` (笔者使用一个IP代理隧道,若需要使用普通IP代理,
资源推荐
资源详情
资源评论
收起资源包目录
Zhihu-Spider-知乎爬虫.zip (49个子文件)
Zhihu-Spider-知乎爬虫
.DS_Store 6KB
spider
crawl.py 4KB
run.py 7KB
datafile.py 11KB
proxy.py 697B
.gitattributes 33B
image
request.png 72KB
proxytunnel.png 52KB
datastate.png 89KB
proxy.png 51KB
flow.png 336KB
file.png 82KB
run.jpg 93KB
datafilelist.png 62KB
datafile.png 60KB
analysis
cloud.ipynb 7KB
image
3D关注和被关注.png 342KB
收藏和被收藏.png 214KB
major.png 677KB
地理分布.png 328KB
thankedCount.png 900KB
mask1.png 373KB
3D关注和被关注.gif 3.15MB
answerCount.png 931KB
问题话题收藏夹专栏.png 203KB
回答文章提问.png 136KB
school.png 655KB
questionCount.png 885KB
关注和被关注.png 179KB
mask2.png 185KB
3D收藏和被收藏.png 278KB
followerCount.png 847KB
3D赞同与感谢.png 267KB
articlesCount.png 921KB
job.png 671KB
赞同和感谢.png 181KB
business.png 541KB
voteupCount.png 910KB
company.png 672KB
map.png 88KB
favoritedCount.png 873KB
3D赞同和感谢.gif 1.95MB
heat.ipynb 25KB
hist3d.ipynb 7KB
datawash.py 3KB
hist.ipynb 18KB
fonts
fangzhengqingkebenyuesongjianti.ttf 3MB
requirments.txt 80B
README.md 13KB
共 49 条
- 1
资源评论
码农飞哥
- 粉丝: 14w+
- 资源: 1767
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功