1.第一個小爬蟲只能爬取指定的列表頁的文章,接下來要自動爬取每一列表頁的文章
2.循環爬取process是會循環運行的。其中的循環並不是for循環,而是利用if
public void process(Page page) { //列表頁 if (page.getUrl().regex(URL_LIST).match()) { System.out.println("列表頁"); /*從頁面發現后續的url地址來抓取 .xpath("//@href") //提取鏈接 .regex(".*blog.*") //正則匹配過濾*/ page.addTargetRequests(page.getHtml().xpath("//div[@class=\"articleList\"]").links().regex(URL _POST).all()); page.addTargetRequests(page.getHtml().links().regex(URL_LIST).all()); //文章頁 } else { System.out.println("文章頁"); //抓取文章頁內容 page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2")); }
3.PageProcess中的site方法是抓取網站的相關配置,包括編碼、抓取間隔、重試次數等
.me() .setDomain("www.ccgp-hebei.gov.cn") .setSleepTime(3000) .setUserAgent( "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");