知乎爬蟲項目記錄

這是一個知乎爬蟲項目

項目地址 https://github.com/rensuperk/javaSpider

根據(jù)知乎用戶爬取所有和用戶相關(guān)的數(shù)據(jù)

數(shù)據(jù)包括關(guān)注者,被關(guān)注者,文章,專欄,想法,提問,答案,和用戶相關(guān)的其他信息

應(yīng)的框架有

  1. jdk1.8
  2. spring-boot --基本架構(gòu)
  3. spring-data-elastic --elastic客戶端
  4. elasticsearch --搜索引擎,也用于存儲數(shù)據(jù)
  5. kinaba --數(shù)據(jù)展示和分析
  6. 爬取網(wǎng)頁用的spring提供的restTemplate

說明

  1. 使用spring的resttemplate抓去知乎的接口
  2. 使用數(shù)據(jù)線程池實現(xiàn)多線程抓取
  3. 記錄上次停頓的位置實現(xiàn)連續(xù)抓取
  4. 控制抓取的數(shù)據(jù)類型
  5. 去重使用的是elsstic提供的upset方法,id的話,people使用url_token
  6. 當(dāng)訪問被限制的時候切換本地的http代理,不想找線程池,所以一般只開兩個線程進行抓取,不得不說知乎反爬蟲做的挺好,只開一個線程的時候不會被屏蔽,多了就不行了
  7. 跑了一個星期大概抓取了200W的用戶數(shù)據(jù)和其他的一些
  8. 先爬取幾條記錄,然后代碼就會自動把記錄寫入任務(wù)隊列中
  9. 使用java.util.concurrent里面的很多包進行多線程訪問的限制,比如緩存線程池,阻塞隊列,atomic原子操作類等

安裝和配置

  • elasticSearch5.5
    cd /apps/
    #下載
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.0.zip
    #解壓縮
    unzip
    cd elasticsearch-5.6.0
    #改變配置
    vim config/elasticsearch.yml
    #配置外網(wǎng)訪問
    network.host : 0.0.0.0
    #cluster名稱
    cluster.name: elasticsearch
    #節(jié)點名稱
    node.name: node-1
    #啟動 bin/elasticsearch -d
    #注意因為安全設(shè)置elasticsearch不允許用root用戶訪問,所以新建一個用戶用于啟動這個服務(wù)
    
  • kibana安裝
#和上面差不多,略過了
  • jdk1.8安裝
#詳見網(wǎng)上ubuntu安裝jdk的教程,有很多,就不貼了
  • spring-boot版本的選擇
elasticsearch更新比較快
springboot-release版本可能不太適配
現(xiàn)在只能使用2.0.0.M3版本
如果用在正式環(huán)境中,可能需要降低elasticsearch,或者再等幾個月等springboot的適配
  • pom.xml
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M3</version>
    </parent>
     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • 配置application.properties
spring.data.elasticsearch.cluster-nodes=192.168.56.101:9300
server.port=80
logging.level.root=info
logging.file=D://log.log

  • 啟動類和一些配置
@SpringBootApplication(scanBasePackages = "ren.superk")
@EnableElasticsearchRepositories(basePackages = "ren.superk")
public class ElasticSearchApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ElasticSearchApplication.class,args);
    }


    @Autowired
    private PeopleService peopleService;
    @Override
    public void run(String... strings) throws Exception {
        peopleService.initDataByThreadCount(2);
    }
}


@Configuration
public class RestTemplateConfig{
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//ms
        factory.setConnectTimeout(15000);//ms
        return factory;
    }
}
  • 文件路徑
使用命令行工具 tree /F .\springboot-spider\src\main > d:/tree.txt 

├─java
│  └─ren
│      └─superk
│          └─zhihu
│              ├─action
│              │      PeopleAction.java    --action接口,寫了3個方法
│              │      
│              ├─config                      -- 配置
│              │      ElasticSearchApplication.java
│              │      ElasticSearchConfig.java
│              │      RestTemplateConfig.java
│              │      
│              ├─core                        --核心枚舉,和類型相關(guān)的參數(shù)都存在美劇中
│              │      SortEnum.java
│              │      ZhihuEnum.java
│              │      
│              ├─model                       --實體類
│              │      Answer.java
│              │      Article.java
│              │      Columns.java
│              │      Education.java
│              │      Employment.java
│              │      Fav.java
│              │      People.java
│              │      Pins.java
│              │      Question.java
│              │      Relation.java
│              │      Topic.java
│              │      ZhihuAnswerPager.java  --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuArticlePager.java --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuColumnsPager.java --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuFavPager.java     --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuPager.java
│              │      ZhihuPeoplePager.java  --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuPinsPager.java    --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuQuestionPager.java --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      ZhihuTopicPager.java  --這幾個本來設(shè)計的有用,但廢棄掉了
│              │      
│              ├─repository
│              │      PeopleRepository.java  --elasticsearch-spring-data的從IDUS方法
│              │      RelationRepository.java
│              │      
│              └─service
│                  │  PeopleService.java    --服務(wù)
│                  │  PeopleUrlService.java --網(wǎng)絡(luò)服務(wù)
│                  │  
│                  └─impl
│                          PeopleServiceImpl.java  -- 主要的方法都寫在這里面
│                          PeopleUrlServiceImpl.java
│                          test.java
│                          
└─resources
        application.properties  --配置
日志
日志
索引
索引

一些簡單的分析,見笑了

索引
索引

關(guān)注的人最多的人


索引
索引

被關(guān)注的人最多的人

索引
索引

感謝最多的

索引
索引

學(xué)校 比較有趣

索引
索引

回答問題最多,真勞模

索引
索引

提問最多

索引
索引

提問最多的主題,有趣


索引
索引

職位,也很有趣

看圖把,我不貼了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,271評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評論 25 709
  • 韜聲折舊閱讀 177評論 0 0
  • “長生,你答應(yīng)過我的,啊,你說過的會帶我走” “青花,不是我不帶你走,是我沒辦法呀,青花,你就原諒我,忘了我吧” ...
    許多霧靄閱讀 1,096評論 6 11

友情鏈接更多精彩內(nèi)容