<div>
<a target="_self" href="http://news.qq.com/" class="b">新闻</a><a target="_self" href="http://v.qq.com/">视频</a><a target="_self" href="http://pp.qq.com/">图片</a><a target="_self" href="http://view.news.qq.com/">评论</a><br>
<a target="_self" href="http://finance.qq.com/" class="b">财经</a><a target="_self" href="http://finance.qq.com/stock/">股票</a><a target="_self" href="http://finance.qq.com/hk/">港股</a><a target="_self" href="http://finance.qq.com/fund/">基金</a></div>
<div class="line"></div>
<div>
<a target="_self" href="http://ent.qq.com/" class="b">娱乐</a><a target="_self" href="http://ent.qq.com/star/">明星</a><a target="_self" href="http://ent.qq.com/movie/">电影</a><a target="_self" href="http://yue.qq.com/">音乐</a><br>
<a target="_self" href="http://sports.qq.com/" class="b">体育</a><a target="_self" href="http://sports.qq.com/nba/" style="font-family:Arial;padding-top:0;padding-bottom:3px;">NBA</a><a href="http://sports.qq.com/lottery/" target="_self">彩票</a><a href="http://2012.qq.com/" target="_self">奥运</a></div>
<div class="line"></div>
<div>
<a target="_self" href="http://auto.qq.com/" class="b">汽车</a><a target="_self" href="http://data.auto.qq.com/car_brand/index.shtml">车型</a><a target="_self" href="http://house.qq.com/">房产</a><a target="_self" href="http://hea.qq.com/">家电</a><br>
<a target="_self" href="http://tech.qq.com/" class="b">科技</a><a target="_self" href="http://digi.tech.qq.com/">数码</a><a target="_self" href="http://digi.tech.qq.com/mobile/">手机</a><a target="_self" href="http://download.tech.qq.com/">下载</a></div>
<div class="line"></div>
<div>如上代码:
用程序可以取出红色标记的数据吗??
有代码更好
<a target="_self" href="http://news.qq.com/" class="b">新闻</a><a target="_self" href="http://v.qq.com/">视频</a><a target="_self" href="http://pp.qq.com/">图片</a><a target="_self" href="http://view.news.qq.com/">评论</a><br>
<a target="_self" href="http://finance.qq.com/" class="b">财经</a><a target="_self" href="http://finance.qq.com/stock/">股票</a><a target="_self" href="http://finance.qq.com/hk/">港股</a><a target="_self" href="http://finance.qq.com/fund/">基金</a></div>
<div class="line"></div>
<div>
<a target="_self" href="http://ent.qq.com/" class="b">娱乐</a><a target="_self" href="http://ent.qq.com/star/">明星</a><a target="_self" href="http://ent.qq.com/movie/">电影</a><a target="_self" href="http://yue.qq.com/">音乐</a><br>
<a target="_self" href="http://sports.qq.com/" class="b">体育</a><a target="_self" href="http://sports.qq.com/nba/" style="font-family:Arial;padding-top:0;padding-bottom:3px;">NBA</a><a href="http://sports.qq.com/lottery/" target="_self">彩票</a><a href="http://2012.qq.com/" target="_self">奥运</a></div>
<div class="line"></div>
<div>
<a target="_self" href="http://auto.qq.com/" class="b">汽车</a><a target="_self" href="http://data.auto.qq.com/car_brand/index.shtml">车型</a><a target="_self" href="http://house.qq.com/">房产</a><a target="_self" href="http://hea.qq.com/">家电</a><br>
<a target="_self" href="http://tech.qq.com/" class="b">科技</a><a target="_self" href="http://digi.tech.qq.com/">数码</a><a target="_self" href="http://digi.tech.qq.com/mobile/">手机</a><a target="_self" href="http://download.tech.qq.com/">下载</a></div>
<div class="line"></div>
<div>如上代码:
用程序可以取出红色标记的数据吗??
有代码更好
解决方案 »
- 求助:java解析zip文件,由于解压目录造成的问题。
- 关于selector+多线程io的使用~~~求思路
- 如何将string转为byte
- 通过Hibernate向数据库里插数据,数据库里的自动增长列怎么办
- Eclipse(3.1)+Lomboz+JBoss4.0.2部署EJB时的困惑, 又一个EJB不眠之夜, 望睡醒的人们给以指点!!!感激~~~~~~~~
- 高手指导下(急)这段代码的语句的意思
- java串口通讯
- 在IDEA中启动tomcat时出错:
- CSDN和清华BBS中关于JBOSS+SSL建立服务端客户端互相握手机制的超难问题我给解决了,进者就有分
- 郁闷中,总是做不好JTable.请高手指点!!在线!!
- 如何在javascript中调用java类并得到java类返回的值?
- 菜菜SOS:JSP中${words}无法读出来,空白
jsoup 是 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}也可以模仿这个解析器用循环截取的方式取出你要的链接和文字。不过这个就麻烦多了
public static void main(String[] args) {
//截取出字符串中的链接和文字部分---最好用html解析
String strs2="<a target='_self' href='http://news.qq.com/' class='b'>新闻</a><a target='_self' href='http://v.qq.com/'>视频</a><a target='_self' href='http://pp.qq.com/'>图片</a><a target='_self' href='http://view.news.qq.com/'>评论</a>";
String len[] = strs2.split("href");
for(int i=1;i<len.length;i++){
//System.out.println(len[i]);
if(len[i].contains("http")){
//System.out.println(len[i].indexOf(">"));
//System.out.println(len[i].indexOf("/"));
String str = len[i].toString().substring((len[i].toString().indexOf("'")+1),(len[i].toString().indexOf("/'")));
String strval = len[i].toString().substring((len[i].indexOf("'>"))+2,(len[i].indexOf("</")));
System.out.println(str);
System.out.println(strval);
}
} }
}
String source ="<div>\n" +
"<a target=\"_self\" href=\"http://news.qq.com/\" class=\"b\">新闻</a><a target=\"_self\" href=\"http://v.qq.com/\">视频</a><a target=\"_self\" href=\"http://pp.qq.com/\">图片</a><a target=\"_self\" href=\"http://view.news.qq.com/\">评论</a><br>\n" +
"<a target=\"_self\" href=\"http://finance.qq.com/\" class=\"b\">财经</a><a target=\"_self\" href=\"http://finance.qq.com/stock/\">股票</a><a target=\"_self\" href=\"http://finance.qq.com/hk/\">港股</a><a target=\"_self\" href=\"http://finance.qq.com/fund/\">基金</a></div>" ;
Pattern pattern=Pattern.compile("<a[\\s\\S]*?href=\"(.*?)\"[\\s\\S]*?>([\\s\\S]*?)</a>");
Matcher matcher=pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group(1)+"=>"+matcher.group(2));
}
输出
http://news.qq.com/=>新闻
http://v.qq.com/=>视频
http://pp.qq.com/=>图片
http://view.news.qq.com/=>评论
http://finance.qq.com/=>财经
http://finance.qq.com/stock/=>股票
http://finance.qq.com/hk/=>港股
http://finance.qq.com/fund/=>基金
我还是觉得这个用html解析器更好点!
var urlText = [];
$('A').each(function(index){
urls.push($(this).attr('href'));
urlText.push($(this).html())
})
$(document).ready(function(){
//输出链接
alert($(this).attr('href'));
});