我想在Java中获取Twitter page title
,description
和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);
}
}
}
先谢谢你。
答案 0 :(得分:1)
如果您要获取的页面已经是UTF-8,那么使用也会接受Charset的重载InputStreamReader
构造函数。使用UTF-8,你应该没问题。