1. 程式人生 > >部落格搬家系列(三)-爬取部落格園部落格

部落格搬家系列(三)-爬取部落格園部落格

部落格搬家系列(三)-爬取部落格園部落格

一.前情回顧

 部落格搬家系列(一)-簡介:https://blog.csdn.net/rico_zhou/article/details/83619152

 部落格搬家系列(二)-爬取CSDN部落格:https://blog.csdn.net/rico_zhou/article/details/83619509

 部落格搬家系列(四)-爬取簡書文章:https://blog.csdn.net/rico_zhou/article/details/83619538

 部落格搬家系列(五)-爬取開源中國部落格:https://blog.csdn.net/rico_zhou/article/details/83619561

 部落格搬家系列(六)-爬取今日頭條文章:https://blog.csdn.net/rico_zhou/article/details/83619564

 部落格搬家系列(七)-本地WORD文件轉HTML:https://blog.csdn.net/rico_zhou/article/details/83619573

 部落格搬家系列(八)-總結:https://blog.csdn.net/rico_zhou/article/details/83619599
 

二.開幹(獲取文章URL集合)

爬取部落格園的部落格思路跟CSDN一樣,且下載圖片那一步更為簡單,任何header都不需要設定,同樣,我們以ricozhou的主頁為例分析原始碼

https://www.cnblogs.com/ricozhou/ 

我們可以看到文章列表如下,依然是很簡潔的url,我們找一個博主文章較多的看看,方便分析規律,如https://www.cnblogs.com/xdp-gacl/

當我們點選下一頁的時候,url如下

顯然最後的2是頁數,這樣我們就找到了頁面url規律,同樣右擊檢視原始碼,分析找到文章都位於哪個標籤內

觀察發現,文章url均位於class為postTitle的標籤內,程式碼如下:

/**
	 * @date Oct 17, 2018 12:30:46 PM
	 * @Desc
	 * @param blogMove
	 * @param oneUrl
	 * @return
	 * @throws IOException
	 * @throws MalformedURLException
	 * @throws FailingHttpStatusCodeException
	 */
	public void getCnBlogArticleUrlList(Blogmove blogMove, String oneUrl, List<String> urlList)
			throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		// 模擬瀏覽器操作
		// 建立WebClient
		WebClient webClient = new WebClient(BrowserVersion.CHROME);
		// 關閉css程式碼功能
		webClient.getOptions().setThrowExceptionOnScriptError(false);
		webClient.getOptions().setCssEnabled(false);
		// 如若有可能找不到檔案js則加上這句程式碼
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
		// 獲取第一級網頁html
		HtmlPage page = webClient.getPage(oneUrl);
		// System.out.println(page.asXml());
		Document doc = Jsoup.parse(page.asXml());
		Elements pageMsg = doc.select("div.postTitle");
		Element linkNode;
		for (Element e : pageMsg) {
			linkNode = e.select("a.postTitle2").first();
			if (linkNode == null) {
				continue;
			}
			if (urlList.size() < blogMove.getMoveNum()) {
				urlList.add(linkNode.attr("href"));
			} else {
				break;
			}
		}
		return;
	}

獲取url集合如下

三.開幹(獲取文章具體資訊)

同樣,我們還是開啟一篇博文,以使用爬蟲框架htmlunit整合springboot出現的一個不相容問題 為例,使用Chrome開啟,我們可以看到一些基本資訊,如文章的型別為原創,標題,時間,作者,閱讀數,文章文字資訊,圖片資訊等

同樣,右擊檢視原始碼找到對應的元素,然後獲取內容

部分程式碼

/**
	 * @date Oct 17, 2018 12:46:52 PM
	 * @Desc 獲取詳細資訊
	 * @param blogMove
	 * @param url
	 * @return
	 * @throws IOException
	 * @throws MalformedURLException
	 * @throws FailingHttpStatusCodeException
	 */
	public Blogcontent getCnBlogArticleMsg(Blogmove blogMove, String url, List<Blogcontent> bList)
			throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		Blogcontent blogcontent = new Blogcontent();
		blogcontent.setArticleSource(blogMove.getMoveWebsiteId());
		// 模擬瀏覽器操作
		// 建立WebClient
		WebClient webClient = new WebClient(BrowserVersion.CHROME);
		// 關閉css程式碼功能
		webClient.getOptions().setThrowExceptionOnScriptError(false);
		webClient.getOptions().setCssEnabled(false);
		// 如若有可能找不到檔案js則加上這句程式碼
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
		// 獲取第一級網頁html
		HtmlPage page = webClient.getPage(url);

		Document doc = Jsoup.parse(page.asXml());
		// 獲取標題
		String title = BlogMoveCnBlogUtils.getCnBlogArticleTitle(doc);
		// 是否重複去掉
		if (blogMove.getMoveRemoveRepeat() == 0) {
			// 判斷是否重複
			if (BlogMoveCommonUtils.articleRepeat(bList, title)) {
				return null;
			}
		}
		blogcontent.setTitle(title);
		// 獲取作者
		blogcontent.setAuthor(BlogMoveCnBlogUtils.getCnBlogArticleAuthor(doc));
		// 獲取時間
		if (blogMove.getMoveUseOriginalTime() == 0) {
			blogcontent.setGtmCreate(BlogMoveCnBlogUtils.getCnBlogArticleTime(doc));
		} else {
			blogcontent.setGtmCreate(new Date());
		}
		blogcontent.setGtmModified(new Date());
		// 獲取型別
		blogcontent.setType(BlogMoveCnBlogUtils.getCnBlogArticleType(doc));
		// 獲取正文
		blogcontent.setContent(BlogMoveCnBlogUtils.getCnBlogArticleContent(doc, blogMove, blogcontent));

		// 設定其他
		blogcontent.setStatus(blogMove.getMoveBlogStatus());
		blogcontent.setBlogColumnName(blogMove.getMoveColumn());
		// 特殊處理
		blogcontent.setArticleEditor(blogMove.getMoveArticleEditor());
		blogcontent.setShowId(DateUtils.format(new Date(), DateUtils.YYYYMMDDHHMMSSSSS));
		blogcontent.setAllowComment(0);
		blogcontent.setAllowPing(0);
		blogcontent.setAllowDownload(0);
		blogcontent.setShowIntroduction(1);
		blogcontent.setIntroduction("");
		blogcontent.setPrivateArticle(1);

		return blogcontent;
	}

詳細資訊

/**
	 * @date Oct 17, 2018 1:10:19 PM
	 * @Desc 獲取標題
	 * @param doc
	 * @return
	 */
	public static String getCnBlogArticleTitle(Document doc) {
		// 標題
		Element pageMsg2 = doc.select("div#post_detail").first().select("h1.postTitle").first().select("a").first();
		return pageMsg2.ownText();
	}

	/**
	 * @date Oct 17, 2018 1:10:28 PM
	 * @Desc 獲取作者
	 * @param doc
	 * @return
	 */
	public static String getCnBlogArticleAuthor(Document doc) {
		Element pageMsg2 = doc.select("div.postDesc").first().select("a").first();
		return pageMsg2.ownText();
	}

	/**
	 * @date Oct 17, 2018 1:10:33 PM
	 * @Desc 獲取時間
	 * @param doc
	 * @return
	 */
	public static Date getCnBlogArticleTime(Document doc) {
		Element pageMsg2 = doc.select("div.postDesc").first().select("span#post-date").first();
		String date = pageMsg2.ownText().trim();
		// 這地方時間格式變化太多暫時不實現
		Date d = DateUtils.formatStringDate(date, DateUtils.YYYY_MM_DD_HH_MM_SS4);
		// 注意有些格式不正確
		return d == null ? new Date() : d;
	}

	/**
	 * @date Oct 17, 2018 1:10:37 PM
	 * @Desc 獲取型別
	 * @param doc
	 * @return
	 */
	public static String getCnBlogArticleType(Document doc) {
		// Element pageMsg2 =
		// doc.select("div.article-detail").first().select("h1.header").first().select("div.horizontal")
		// .first();
		// if ("原".equals(pageMsg2.html())) {
		// return "原創";
		// } else if ("轉".equals(pageMsg2.html())) {
		// return "轉載";
		// } else if ("譯".equals(pageMsg2.html())) {
		// return "翻譯";
		// }
		return "原創";
	}
/**
	 * @date Oct 17, 2018 1:10:41 PM
	 * @Desc 獲取正文
	 * @param doc
	 * @param object
	 * @param blogcontent
	 * @return
	 */
	public static String getCnBlogArticleContent(Document doc, Blogmove blogMove, Blogcontent blogcontent) {
		Element pageMsg2 = doc.select("div#post_detail").first().select("div#cnblogs_post_body").first();
		String content = pageMsg2.toString();
		String images;
		// 注意是否需要替換圖片
		if (blogMove.getMoveSaveImg() == 0) {
			// 儲存圖片到本地
			// 先獲取所有圖片連線,再按照每個連結下載圖片,最後替換原有連結
			// 先建立一個資料夾
			// 先建立一個臨時資料夾
			String blogFileName = String.valueOf(UUID.randomUUID());
			FileUtils.createFolder(FilePathConfig.getUploadBlogPath() + File.separator + blogFileName);
			blogcontent.setBlogFileName(blogFileName);
			// 匹配出所有連結
			List<String> imgList = BlogMoveCommonUtils.getArticleImgList(content);
			// 下載並返回重新生成的imgurllist
			List<String> newImgList = BlogMoveCommonUtils.getArticleNewImgList(blogMove, imgList, blogFileName);
			// 拼接文章所有連結
			images = BlogMoveCommonUtils.getArticleImages(newImgList);
			blogcontent.setImages(images);
			// 替換所有連結按順序
			content = getCnBlogNewArticleContent(content, imgList, newImgList);

		}

		return content;
	}

程式碼共用,不再多放。還是一樣的步驟,獲取正文原始碼html匹配img,下載img,替換img連結,返回替換後的HTML

本人網站效果圖:

歡迎交流學習!

完整原始碼請見github: