Last active
August 29, 2015 13:56
-
-
Save dongguosheng/9019464 to your computer and use it in GitHub Desktop.
第一次使用jsoup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test_jsoup.test_jsoup; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.nodes.Element; | |
import org.jsoup.select.Elements; | |
public class App | |
{ | |
public static void main( String[] args ) throws Exception | |
{ | |
final String urlHead = "http://bbs.byr.cn"; | |
final int pageNum = 1; | |
Document doc = Jsoup.connect("http://bbs.byr.cn/board/ParttimeJob?_uid=guest") | |
.header("X-Requested-With", "XMLHttpRequest") | |
.data("_uid", "guest") | |
.data("p", Integer.toString(pageNum)) | |
.get(); | |
// String title = doc.title(); | |
// System.out.println(doc.html()); | |
//标题,链接,发帖日期,内容(通过链接获取) | |
Elements links = doc.select("tbody").select("tr"); | |
// System.out.println(title); | |
for(Element link : links){ | |
String partURL = link.select(".title_9").select("a[href]").attr("href"); | |
System.out.print(link.select(".title_9").text()); | |
System.out.println(link.select(".title_10").first().text()); | |
System.out.println(getContent(urlHead + partURL)); | |
// System.out.println(link.attr("href")); | |
} | |
} | |
public static String getContent(String url) throws Exception { | |
Document doc = Jsoup.connect(url) | |
.header("X-Requested-With", "XMLHttpRequest") | |
.data("_uid", "guest") | |
.get(); | |
// System.out.println(doc.html()); | |
Elements links = doc.select(".a-content"); | |
// System.out.println(links.first().text()); | |
//内容的格式调整 | |
return links.first().html(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment