html提取pdf,使用jsoup从HTML提取链接并用pdf显示

提取HTML用jsoup-1.6.1.jar

用pdf显示用itextpdf-5.1.1.jar

1.[代码][Java]代码

package jsoup;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

import com.itextpdf.text.Anchor;

import com.itextpdf.text.BaseColor;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Font;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfWriter;

public class Cnblogs {

public static void main(String[] args) {

int page = 20;

org.jsoup.nodes.Document doc;

com.itextpdf.text.Document pdf = new Document(PageSize.A4.rotate(), 50,

50, 50, 50);

try {

PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(

"G:\\cnblogs.pdf"));

pdf.open();

BaseFont zh_cn = BaseFont.createFont(

"C:\\WINDOWS\\Fonts\\msyh.ttf", "Identity-H",

BaseFont.NOT_EMBEDDED);

for (int p = 1; p <= page; p++) {

if (p == 1) {

doc = Jsoup.connect("http://www.cnblogs.com/").get();

} else {

doc = Jsoup.connect("http://www.cnblogs.com/p" + p).get();

}

Elements elements = doc.body().getElementsByClass(

"post_item_body");

for (Element e : elements) {

Elements titleEle = e.getElementsByClass("titlelnk");

String titleLink = titleEle.attr("href");

String titleText = titleEle.text();

Elements summaryEle = e

.getElementsByClass("post_item_summary");

String summary = summaryEle.text();

Anchor anchor = new Anchor(titleText, new Font(zh_cn, 14,

Font.UNDERLINE, BaseColor.BLUE));

anchor.setReference(titleLink);

Paragraph titlePar = new Paragraph();

titlePar.add(anchor);

Paragraph summaryPar = new Paragraph(summary, new Font(

zh_cn, 12));

summaryPar.setFirstLineIndent(24);

pdf.add(titlePar);

pdf.add(summaryPar);

pdf.add(new Paragraph(" "));

}

}

pdf.close();

writer.close();

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (DocumentException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2.[代码][Java]代码

package jsoup;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

import com.itextpdf.text.Anchor;

import com.itextpdf.text.BaseColor;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Font;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfWriter;

public class CsdnBlog {

public static void main(String[] args) {

int page = 20;

org.jsoup.nodes.Document doc;

com.itextpdf.text.Document pdf = new Document(PageSize.A4.rotate(), 50,

50, 50, 50);

try {

PdfWriter writer = PdfWriter.getInstance(pdf, new FileOutputStream(

"G:\\csdn.pdf"));

pdf.open();

BaseFont zh_cn = BaseFont.createFont(

"C:\\WINDOWS\\Fonts\\msyh.ttf", "Identity-H",

BaseFont.NOT_EMBEDDED);

for (int p = 1; p <= page; p++) {

doc = Jsoup

.connect("http://blog.csdn.net/hot.html?page=" + p)

.header("User-Agent",

"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2")

.get();

Elements elements = doc.body().getElementsByClass("blog_list");

for (Element e : elements) {

Elements titleEle = e.getElementsByTag("h1");

String titleLink = titleEle.last().attr("href");

String titleText = titleEle.last().text();

Elements summaryEle = e.getElementsByTag("dd");

String summary = summaryEle.text();

Anchor anchor = new Anchor(titleText, new Font(zh_cn, 14,

Font.UNDERLINE, BaseColor.BLUE));

anchor.setReference(titleLink);

Paragraph titlePar = new Paragraph();

titlePar.add(anchor);

Paragraph summaryPar = new Paragraph(summary, new Font(

zh_cn, 12));

summaryPar.setFirstLineIndent(24);

pdf.add(titlePar);

pdf.add(summaryPar);

pdf.add(new Paragraph(" "));

}

}

pdf.close();

writer.close();

} catch (FileNotFoundException e1) {

e1.printStackTrace();

} catch (DocumentException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

3.[图片] 截图00.png

4.[图片] 截图01.png

你可能感兴趣的:(html提取pdf)