java微博爬虫
微博爬取要做到每日百萬級的數據量,需要解決很多問題。
?
1.springboot自帶@Scheduled注解是一個輕量級的quartz,可以完成定時任務。只需要在運行方法上加一個@Scheduled注解即可。
該注解有許多屬性值
initiaDelay 從程序開始延長一定時間后首次執行。
fixedRate ?首次后,該方法固定執行間隔。
cron 定時表達式。
等等
如果想要并發編程,在要執行定時任務的類上加注解@EnableAsync,在方法上加@Async。
接下來要設置線程池的大小。
package com.cnxunao.weibospider.configuration;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar;import java.util.concurrent.Executor; import java.util.concurrent.Executors;@Configuration @EnableScheduling public class ScheduleConfig implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler(taskExecutor());}/** 并行執行*/@Bean(name = "TaskPool")public Executor taskExecutor() {return Executors.newScheduledThreadPool(5);}}最后在啟動類上加一個注解
?
?
2.微博的反爬機制還是蠻嚴格的,親自試驗讓代理服務器每6秒爬取一次是很穩定的,不會被封。
也可以建立一個user_agent池用于反爬。(都是親測可用的)
//user_Agent池private static String[] userAgents = {"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50","Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)","Mozilla/5.0 (Windows; U; Windows NT 6.1; ) AppleWebKit/534.12 (KHTML, like Gecko) Maxthon/3.0 Safari/534.12","Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.33 Safari/534.3 SE 2.X MetaSr 1.0","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1 QQBrowser/6.9.11079.201","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"}; //隨機獲取總數為14useragent池中的一個 private String userAgent = userAgents[random.nextInt(14)];注:隨機獲取1-13的整數?random.nextInt(13)+1 。13位容量,1位初始值。
還可以采用購買大量微博賬號(很便宜)模擬登陸后,獲取cookie,然后構建cookie池的方法。
?
3.購買代理ip的話,需要調用商家的API來獲取代理,具體方法商家官網都有開發者文檔,代碼有些簡單這里就不發出來了。
如果使用springboot的話,自定義配置可以使用@Value來進行賦值。
?application.properties
??java文件
?自動填充效果
?
4.java程序運行時間長后會變慢,可以利用一些設計模式來解決性能的問題,合理使用虛擬機內存空間,即使刪除不用的對象。我也是邊學邊做,現在還沒有完全解決,之后會更新。
5.并發程序有時會造成讀寫數據的錯亂問題,需要了解一些并發編程的規范。還有一種辦法,把需要共享的數據放在redis里,來解決這類問題。
6.如果你覺得微博關鍵字的爬取只能爬取首頁太少,可以在模擬登錄后保存cookie進行爬取,可以爬到所有的相關頁面。
7.編程中日志是很重要的,是反應情況,分析問題,解決問題的關鍵,一定要在適當的程序位置加一些日志信息。下圖是我寫日志的一種方法。
?
轉載于:https://www.cnblogs.com/Post-90sDachenchen/p/11104291.html
總結
- 上一篇: 我们是易网库!
- 下一篇: 洛谷P1558 色板游戏