
Pattern p=Patterncompile(regex,PatternDOTALL);
默认情况下,点并不能匹配换行,所以使用Patterncompile方法时,必须使用PatternDOTALL参数,让点匹配所有字符
一个问题问了N遍,虽然可以有很多答案,只要有一个答案就行了吧?
<a href=">
因为URL采用ISO-8859-1编码,所以得把它转换成与页面相同的编码方式。
获取的值转码试试看
比如:
str=new String(strgetBytes("iso-8859-1"),("gb2312"));
public static String SendGET(String url,String param){
String result="";//访问返回结果
BufferedReader read=null;//读取访问结果
try {
//创建url
URL realurl=new URL(url+""+param);
//打开连接
URLConnection connection=realurlopenConnection();
// 设置通用的请求属性
connectionsetRequestProperty("accept", "/");
connectionsetRequestProperty("connection", "Keep-Alive");
connectionsetRequestProperty("user-agent",
"Mozilla/40 (compatible; MSIE 60; Windows NT 51;SV1)");
//建立连接
connectionconnect();
// 获取所有响应头字段
Map<String, List<String>> map = connectiongetHeaderFields();
// 遍历所有的响应头字段,获取到cookies等
for (String key : mapkeySet()) {
Systemoutprintln(key + "--->" + mapget(key));
}
// 定义 BufferedReader输入流来读取URL的响应
read = new BufferedReader(new InputStreamReader(
connectiongetInputStream(),"UTF-8"));
String line;//循环读取
while ((line = readreadLine()) != null) {
result += line;
}
} catch (IOException e) {
eprintStackTrace();
}finally{
if(read!=null){//关闭流
try {
readclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
return result;
}
你得先查看sina微博空间的用户日志页面的源代码以及用正则表达式表示它通配的URL,这样你就可以抓取到你想要的东西了,我这边做了一个抓取sina,网易,yahoo网站上的新闻资讯,需要给我发邮件,我邮箱:yghsxdr2009@163com
以上就是关于JAVA正则表达式获取网页内容的问题全部的内容,包括:JAVA正则表达式获取网页内容的问题、求助:如何从网页中提取需要的数据,用JAVA实现、怎样用JAVA实现模拟HTTP请求,得到服务器的响应时间等参数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)