Created
September 27, 2013 14:02
-
-
Save Gaubee/6729062 to your computer and use it in GitHub Desktop.
Java 抓取Qzone的说说数据的实例
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class qzone { | |
/** | |
* 读取网页全部内容 | |
*/ | |
public String getHtmlContent(String htmlurl) { | |
URL url; | |
String temp; | |
StringBuffer sb = new StringBuffer(); | |
try { | |
url = new URL(htmlurl); | |
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); // 读取网页全部内容 | |
while ((temp = in .readLine()) != null) { | |
sb.append(temp); | |
} in .close(); | |
} catch (final MalformedURLException me) { | |
System.out.println("Error URL Format!"); | |
me.getMessage(); | |
} catch (final IOException e) { | |
e.printStackTrace(); | |
} | |
return sb.toString(); | |
} | |
/** | |
* | |
* @param s | |
* @return 获得网页标题 | |
*/ | |
public String getTitle(String s) { | |
String regex; | |
String title = ""; | |
List < String > list = new ArrayList < String > (); | |
regex = "<title>.*?</title>"; | |
Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ); | |
Matcher ma = pa.matcher(s); | |
while (ma.find()) { | |
list.add(ma.group()); | |
} | |
for (int i = 0; i < list.size(); i++) { | |
title = title + list.get(i); | |
} | |
return outTag(title); | |
} | |
/** | |
* | |
* @param s | |
* @return 获得链接 | |
*/ | |
public List < String > getLink(String s) { | |
String regex; | |
List < String > list = new ArrayList < String > (); | |
regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>"; | |
Pattern pa = Pattern.compile(regex, Pattern.DOTALL); | |
Matcher ma = pa.matcher(s); | |
while (ma.find()) { | |
list.add(ma.group()); | |
} | |
return list; | |
} | |
/** | |
* | |
* @param s | |
* @return 获得脚本代码 | |
*/ | |
public List < String > getScript(String s) { | |
String regex; | |
List < String > list = new ArrayList < String > (); | |
regex = "<SCRIPT.*?</SCRIPT>"; | |
Pattern pa = Pattern.compile(regex, Pattern.DOTALL); | |
Matcher ma = pa.matcher(s); | |
while (ma.find()) { | |
list.add(ma.group()); | |
} | |
return list; | |
} | |
public List < String > getNews(String s) { | |
String regex = "<a.*?</a>"; | |
Pattern pa = Pattern.compile(regex, Pattern.DOTALL); | |
Matcher ma = pa.matcher(s); | |
List < String > list = new ArrayList < String > (); | |
while (ma.find()) { | |
list.add(ma.group()); | |
} | |
return list; | |
} | |
/** | |
* | |
* @param s | |
* @return 获得CSS | |
*/ | |
public List < String > getCSS(String s) { | |
String regex; | |
List < String > list = new ArrayList < String > (); | |
regex = "<style.*?</style>"; | |
Pattern pa = Pattern.compile(regex, Pattern.DOTALL); | |
Matcher ma = pa.matcher(s); | |
while (ma.find()) { | |
list.add(ma.group()); | |
} | |
return list; | |
} | |
/** | |
* | |
* @param s | |
* @return 去掉标记 | |
*/ | |
public String outTag(final String s) { | |
return s.replaceAll("<.*?>", ""); | |
} | |
public static void main(String[] args) { | |
qzone t = new qzone(); | |
String content = t.getHtmlContent("http://taotao.qq.com/cgi-bin/emotion_cgi_homepage_msg?owneruin=654393589&start=0&num=10&code_version=1&format=jsonp&g_tk=5381"); | |
System.out.println(content); | |
System.out.println(t.getTitle(content)); | |
List < String > a = t.getNews(content); | |
List < String > news = new ArrayList < String > (); | |
for (String s: a) { | |
news.add(s.replaceAll("<.*?>", "")); | |
} | |
System.out.println(news); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://taotao.qq.com/cgi-bin/emotion_cgi_homepage_msg?owneruin=654393589&start=0&num=10&code_version=1&format=jsonp&g_tk=133572586