我用爬虫一天时间“偷了”知乎一百万用户,只为证明PHP是世界上最好的语言
我用爬虫一天时间“偷了”知乎一百万用户,只为证明PHP是世界上最好的语言
看了不少朋友圈里推荐的Python爬虫文章,都觉得太小儿科,处理内容本来就是PHP的强项,Python唯一的好处估计也就天生的Linux自带, 和Perl一样,这点觉得挺不够意思的Linux,还是Mac厚道,天生就自带了Python、Perl、PHP、Ruby,当然我也很讨厌讨论一门语言 的好坏,每门语言存在就一定有它的道理,反正PHP是全世界最好用的语言,大家都懂的^_^
前几天比较火的是一个人用C#写了一个多线程爬虫程序,抓取了QQ空间3000万QQ用户,其中有300万用户是有QQ号、昵称、空间名称等信息的,也就 是说,有详情也就300万,跑了两周,这没什么,为了证明PHP是全世界最好的语言,虽然大家都懂的^_^,我用PHP写了一个多进程爬虫程序,只用了一 天时间,就抓了知乎100万用户,目前跑到第8圈(depth=8)互相有关联(关注了和关注者)的用户。
爬虫程序设计:
因为知乎需要登录才能获取到关注者页面,所以从chrome登录之后把cookie拷贝下来给curl程序模拟登录。
使用两大独立循环进程组(用户索引进程组、用户详情进程组),用的是php的pcntl扩展,封装了一个非常好用的类,使用起来和golang的携程也差不多了。
下面是用户详情的截图,用户索引代码类似
这里插个题外话,经过测试,我的8核的Macbook,跑16进程的速度最快,而16核的Linux服务器,居然是跑8进程最快,这点有点让我莫名其妙了,不过既然测试出最后进程数,就按照最好的设置就好啦。
1、用户索引进程组先以一个用户为起点,抓取这个用户的关注了和关注者,然后合并入库,因为是多进程,所以当有两个进程在处理同一个用户入库的时候就会出 现重复的用户,所以数据库用户名字段一定要建立唯一索引,当然也可以用redis这些第三方缓存来保证原子性,这个就见仁见智了。
通过步骤一之后,我们就得到下面的用户列表:
2、用户详情进程组按照时间正序,拿到最先入库的用户抓取详情,并且把更新时间更新为当前时间,这样就可以变成一个死循环,程序可以无休止的跑,不断的循环更新用户信息。
程序稳定运行到第二天,突然没有新数据了,检查了一下发现知乎改规则了,不知是为了防我,还是碰巧,反正给我返回的数据是这样的
第一感觉就是胡乱给我输出数据让我采集不到,换了IP、模拟伪装了些数据,都没用,突然感觉这个很熟悉,会不会是gzip?抱着怀疑的态度,试了试gzip,首先当然是告诉知乎不要给我gzip压缩过的数据
把 "Accept-Encoding: gzip,deflate\r\n"; 去掉,然并卵!
看来知乎是强制要给我gzip压缩数据了,既然如此,那我就解压呗,查了一下php解压gzip,发现就一个函数gzinflate,于是把获取到得内容加上:
$content = substr($content, 10);
$content = gzinflate($content));
当然还可以用curl自带的:
curl_setopt( self::$ch, CURLOPT_ENCODING, 'gzip' );
这里我真想说,PHP真的是全世界最好的语言,就一两个函数,就彻底解决了问题,程序又欢快的跑起来了。
在匹配内容的时候,知乎的细心也是给了我无数的帮助,例如我要分清用户性别:
哈哈开玩笑的拉,其实是样式里面有 icon-profile-female 和 icon-profile-male ^_^
我蛋疼的抓了它那么多用户,到底有什么用呢?
其实没什么用,我就是闲的蛋疼 ^_^
有了这些信息,其实就可以做一些别人开头闭口就乱吹一通的大数据分析拉
最常见的当然是:
1、性别分布
2、地域分布
3、职业分布,来自那个公司
4、每个职业的男女比例
5、大家一般在什么时候上知乎?发问题、关注问题,那些问题值得关注
当然,按照关注人数、浏览人数、提问数、回答数等排序,看看人民都在关注什么,民生、社会、地理、政治,整个互联网都尽收眼底拉。。
也许,你还可以把头像拿来分析,用开源的验黄程序,把色情的筛选出来,然后去拯救东莞? ^_^
然后,你还可以看看那些大学出来的人,最后都干了什么。
有了这些数据,是不是可以打开脑洞 ^_^
下面是利用这些数据做出来的一些有趣的图表,实时图表数据可以去 http://www.epooll.com/zhihu/ 上看

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
