Git Product home page Git Product logo

top-topic-zhihu's Introduction

top-topic-Zhihu

抓取「知乎」网站每天提出的热门 top10 问题聚合显示,提供另一种看知乎的姿势。包含前后端整个项目。

世界很大,不被纷繁的 timeline 所迷惑。

步骤

需要这几步来完成目标:

抓取 ——> 存储(数据持久化) ——> 分析 ——> 展示

抓取:抓取部分主要是爬虫,先手动输入验证码获取登录 Cookie。然后带着该 Cookie 模拟发出 Get 请求来获得网页数据。思路是从自己的个人主页开始爬,先爬出现在主页 timeline 上的所有人,再爬这些人主页上的其他人...,直到数据量足够大。把人的 ID 存储在 people 中。接着继续爬 people 中所有人主页上提出的问题,并获得问题的关注人数和提问时间。把抓取到的问题存储在 question 中。

存储:存储可以把上面的 people,question 写入文本或者 MySQL 数据库,。中间数据也应该放到数据库中,不然内存会被无穷多的递归生成的中间数据填满。本项目使用带主键的 MySQL 表模拟内存 set 来存储 people。

分析:网站目的是获取每天或者一个时间段内新提出的 top10 热门问题,所以需要对时间过滤,对关注人数排序。这都可以在 SQL 查询中完成。

展示:展示包括后台和前端两部分,后台需要在 DB 中取得数据构造成 JSON 格式以 CGI 的形式提供给前端调用。这里使用 Python Flask 框架提供 CGI 后台服务。前端页面主要是跨域 AJax 请求后台 CGI 来获得数据,结合定义的模板来展示页面。在版本 V1 中使用 AngularJS 来简单的编写模板及 AJax 请求的逻辑部分,在版本 V2 中使用 artTemplate 和封装原生的 Js 来满足需求。

目录结构

└── top-topic-Zhihu
    ├── assets
    │   └── demo.png
    ├── captcha.gif    # 拉取到本地的验证码
    ├── dataSpider.py  # 爬虫
    ├── dataAccess.py  # AO 服务
    ├── dataCGI.py     # Python Flask 提供给前端的 CGI
    ├── people_db.txt  # 抓到的人
    ├── people_visited_db.txt
    ├── question_db.txt# 抓到的问题
    ├── README.md
    ├── tool           # 工具
    │   └── cron.sh    # 定时任务 每天 23:00 执行 dataSpider.py
    └── www            # 网站文件
        ├── assets    
        │   ├── tuzhii.ico
        │   └── tuzhii.jpg
        ├── css
        │   ├── button.css   # 按钮样式
        │   └── toptopic.css # 网页样式
        ├── index.html
        └── js
            └── template.js  # artTemplate 库

依赖

  • BeautifulSoup
  • requests
  • MySQLdb
  • flask
  • flask.ext.cors

使用方法

  • 本地 MySQL 中建数据库 top_topic_zhihu。dataAccess.py 中 init 构造方法 MySQL 的密码更改为自己的密码

  • dataSpider.py 中 get_login_cookies() 函数中 email 和 password 修改为自己的账户密码,main 中

text = crawl_url(req, local_cookies, 'https://www.zhihu.com/people/your_id')

your_id 修改为自己的用户 ID

  • index.html 中请求的服务器地址 your_ip 修改为自己机器的 IP。dataCGI.py main 下面的 your_ip 也修改为此 IP。
var resource_url = "http://your_ip:5000/toptopic/api/topics/"
  • 执行 dataAccess.py 下列方法建表
    info.create_question_table()
    info.create_people_table()
    info.create_people_merged_table()
  • 执行 dataSpider.py 下列方法抓取 people 数据,需要手动输入本目录下图片中的验证码
    req, local_cookies = get_login_cookies()
    # 第一次获取自己主页的网页
    text = crawl_url(req, local_cookies, 'https://www.zhihu.com/people/your_name')

    # 构造 people 的数据库
    construct_people_db_v2(req, local_cookies, text)
  • 执行 dataAccess.py 下列方法合并 people 表
    info.merge_people_of_db()
  • 执行 dataSpider.py 下列方法抓取 question 数据
    convert_from_people_to_question(req, local_cookies)
  • 运行 dataCGI.py 文件,在浏览器输入 http://127.0.0.1/ 即可访问网站

配置 Nginx

网站写好后需要服务器来提供访问,由于是前后端分离的 SPA(Single Page Application),所以使用 Nginx 提供静态页面的 HTTP 服务。作下面的配置:

  • 在 Nginx 安装目录 /usr/local/nginx/html 下新建一个到网站源码的软链接:

ln -s /your-src/www ./www

把所有的源码放在 /your-src/www 目录。这样便于版本管理。当源码发生更改,只需要更改软链接。

  • 修改 Nginx 配置文件
http {
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
}

root 修改为 html/www,因为上一步是把源码放在了文件夹 www 下。

  • 更改文件和目录权限,以免出现 403 Forbidden 错误。

find ./ -type d | xargs chmod 755

  • 重启 Nginx 服务
cd /usr/local/nginx/sbin
./nginx -c nginx.conf

Demo

image

top-topic-zhihu's People

Contributors

huangtuzhi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

top-topic-zhihu's Issues

知乎反爬虫策略,导致 Requests 库出现 ('Connection aborted.', BadStatusLine(""''''")) 错误

https://github.com/kennethreitz/requests/issues/2364

I usually see this when a proxy terminates a HTTPS connection. It can't
send back headers, because it can't read the encrypted data going back and
forth, so it sends back the empty string "". httplib attempts to parse ""
as "HTTP/1.x " and fails with the above message.

措施为更改爬虫频率

def convert_from_people_to_question(req, local_cookies):
    dbObject = DataInfo()
    all_people = dbObject.get_all_in_people_merged_db()

    for people in all_people:
        homepage_url = "https://www.zhihu.com/people/" + people
        homepage = crawl_url(req, local_cookies, homepage_url)
        soup = BeautifulSoup(homepage)
        for one in soup(class_='question_link'):
            question_id = one.get('href').split('/')[2]

            # 判断 DB 是否已经有了这个 question_id,有了则重新获取别的
            if dbObject.is_question_visited(question_id):
                continue

            question_title = one.string.encode('utf-8')

            question_url = "https://www.zhihu.com/question/" + question_id + "/log"
            time.sleep(2) # 睡眠 300ms,知乎有反爬虫策略
            print question_url
            question_page = crawl_url(req, local_cookies, question_url)

睡眠时间小于 1s 还会出现问题,设成 2s 可以正常工作。

亲,Docker不是虚拟主机..

Docker 只是一个容器,不是vagrant 不是 vmware,
这样高度集成在一个容器内,真的是最佳姿势吗?
好不容易拆分的微服务,又调个头集成到一起真的好吗

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.