我想获取 Twitterpage title
和description
Java keywords
。
为此,我多次凝视,但找不到解决方案。所有人都以某种 ISO-8859 字符集格式回复了我。请帮助我以 UTF-8 字符集格式做出响应。
我为此使用了以下代码,
public class TitDesKey
{
public static void main ( String[] args ) throws IOException
{
String inputLine,source= null,result_tit= null,result_des= null,result_key= null;
try
{
URL url = new URL("http://www.twitter.com");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((inputLine = in.readLine()) != null)
{
source=source+" "+inputLine;
if(inputLine.contains("</head>"))
{
break;
}
}
}
catch (MalformedURLException e)
{
System.out.println("Please Enter Write Information");
}
catch (IOException e)
{
System.out.println("Please Enter Write Information");
}
// Title Data
Pattern PATTERN_tit = Pattern.compile("<title>(.*?)</title>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher m_tit = PATTERN_tit.matcher(source);
while (m_tit.find())
{
result_tit = m_tit.group(1);
result_tit = result_tit.replace("/", "").trim();
System.out.println(result_tit);
}
// Description Data
Pattern Pattern_dis = Pattern.compile("<meta name=\"description\" content=(.*?)>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher m_dis = Pattern_dis.matcher(source);
while (m_dis.find())
{
result_des = m_dis.group(1);
result_des = result_des.replace("/", "").trim();
System.out.println(result_des);
}
// Keyword Data
Pattern Pattern_key = Pattern.compile("<meta name=\"keywords\" content=(.*?)>",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Matcher m_key = Pattern_key.matcher(source);
while (m_key.find())
{
result_key = m_key.group(1);
result_key = result_key.replace("/", "").trim();
System.out.println(result_key);
}
}
}
先感谢您。