http://v.youku.com/v_playlist/f17252787o1p0.html#replyLocation
有一个优酷的视频,想要获取该视频的评论,但是从源码来分析不到这些评论,评论隐藏到一个frame框架。

解决方案 »

  1.   

    使用FF的firebug查看请求的路径,再使用httpclient去抓取
      

  2.   

    我只会firebug简单的功能,具体不知道怎么找出请求路劲,高手指点下
      

  3.   

    <a charset="404-15-2-1" href="http://comments.youku.com/comments/~ajax/vpcommentContent.html" onclick="nova_call(decodeURIComponent(this.href),{'id':'XMzcyNjAwNzM2','sid':354911764,'page':2,'last_modify':1333032010},'displayComments',undefined,1);commentsPage();return false;">2</a>
      

  4.   

    GET /comments/~ajax/vpcommentContent.html?
    参数:__ap={"id":"XMzcyNjAwNzM2","sid":354909363,"page":4,"last_modify":1333031704}
          __callback=displayComments
          __ai=
      

  5.   


    URLConnection conn = new URL("http://comments.youku.com/comments/~ajax/vpcommentContent.html?__ap={%22id%22:%22XMzcyNjAwNzM2%22,%22sid%22:354909363,%22page%22:4,%22last_modify%22:1333031704}&__ai=&__callback=displayComments").openConnection();

    InputStream is = conn.getInputStream();

    OutputStream os = new FileOutputStream("o:\\save.html");

    byte[] buffer = new byte[2048];

    int length = 0;

    while(-1 != (length = is.read(buffer, 0, buffer.length)))
    {
    os.write(buffer, 0, length);
    }

    is.close();
    os.close();
      

  6.   

    我找到了你所说的路径,但是打开找不到评论,读不懂这个路径的文件代表着什么。具体的评论的内容在HTML源码是不能查到的,但是使用firebug可以看到,我就想抓取这个评论的内容。但是本人对js不是很熟悉,所以不知道该怎么深入使用firebug,希望您能具体说下。你上面抓取的url内容里面我找不到评论。谢谢你哦,麻烦您再帮我下
      

  7.   

    你这样直接访问是可能是不行,你使用firebug查看,还有其他很多的参数,比如说一定要从主页进去,那就要先访问一次主页面,然后才能进行分页查看.还有其他很多参数的
      

  8.   


    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod("http://v.youku.com/v_playlist/f17252787o1p0.html#replyLocation");
    GetMethod get2 = new GetMethod("http://comments.youku.com/comments/~ajax/vpcommentContent.html?__ap=%7B%22id%22%3A%22XMzcyOTQ0MTY4%22%2C%22sid%22%3A355016202%2C%22page%22%3A2%2C%22last_modify%22%3A1333080602%7D&__ai=&__callback=displayComments");
    try {
    System.out.println(client.executeMethod(get));
    System.out.println(client.executeMethod(get2));
    System.out.println(get2.getResponseBodyAsString());
    } catch (HttpException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  9.   


    <div class=\"comment\" id=\"comment354915617\">\r\n<div class=\"bar\">\r\n\t <a href=\"http:\/\/u.youku.com\/user_show\/id_UMzI1MzM1MDUy.html\" charset=\"400-4\" target=\"_blank\" id=comment_name_354915617 name=coment_name_354915617>kaka123456782<\/a>\r\n<\/div>\r\n\t<div class=\"con\">\r\n\t\t\t\t\t\t\t\t<div class=\"text\" id=\"content_354915617\" name=\"content_354915617\">\r\n\t\t\t\t\t\t<p id=\"content_4f7475f62e5b94e433000002\">\u90a32\u4e2a\u8001\u4eba\u4e00\u4e2a\u662f\u4e2a\u8001\u6d41\u6c13 \u4e00\u4e2a\u662f\u5f53\u4fdd\u5b89\u90a3\u4e2a\u4e5f\u662f\u6d41\u6c13\u51fa\u8eab\u7684 \u5929\u5929\u6b3a\u8d1f\u90a3\u9ed1\u8863\u670d\u8001\u5988 \u4ed6\u8001\u7238\u6b7b\u7684\u65e9\u5c31\u8001\u5988\u5728\u90a3\u5de5\u5382\u4e0a\u73ed\u5929\u5929\u88ab\u90a3\u8001\u6d41\u6c13\u6027\u68a2\u6270 \u624d\u53eb\u4e0a\u4ed6\u90a3\u8fdc\u623f\u8868\u5f1f\u6765\u5e2e\u5fd9\u7684222222<br \/> <\/p>Unicode编码 可以转成汉字
      

  10.   

    下午分析了半天才知道这个unicode码,早知道早点来看看回复了。可惜内嵌的unicode码转换中文,变成乱码了。单独拿出那你个unicode的中文是可以转化
      

  11.   

    谢谢你。已经取出数据,就差unicode码转化成汉字了
      

  12.   


    package other;import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.methods.GetMethod;public class 获取视频评论 {
    public static String  escape (String src) {  int i;
    char j;
    StringBuffer tmp = new StringBuffer();
    tmp.ensureCapacity(src.length()*6);
    for (i=0;i<src.length() ;i++ )  
    {   
    j = src.charAt(i);
    if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))    
    tmp.append(j);
    else    if (j<256)    { 
    tmp.append( "%" );
    if (j<16)  
    tmp.append( "0" );
    tmp.append( Integer.toString(j,16) );
    }    else    {
    tmp.append( "%u" );
    tmp.append( Integer.toString(j,16) );
    }  
    }  
    return tmp.toString();

    public static String  unescape (String src) { 
    StringBuffer tmp = new StringBuffer();
    tmp.ensureCapacity(src.length());
    int  lastPos=0,pos=0;
    char ch;
    while (lastPos<src.length())  {   pos = src.indexOf("%",lastPos);
    if (pos == lastPos)    {    if (src.charAt(pos+1)=='u')     {     ch = (char)Integer.parseInt(src.substring(pos+2,pos+6),16);
    tmp.append(ch);
    lastPos = pos+6;
    }    else     {     ch = (char)Integer.parseInt(src.substring(pos+1,pos+3),16);
    tmp.append(ch);
    lastPos = pos+3;
    }    }   else    {    if (pos == -1)     {     tmp.append(src.substring(lastPos));
    lastPos=src.length();
    }    else     {     tmp.append(src.substring(lastPos,pos));
    lastPos=pos;
    }    }  }  return tmp.toString();
    }
    public static void main(String[]args){
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod("http://v.youku.com/v_playlist/f17252787o1p0.html#replyLocation");
    GetMethod get2 = new GetMethod("http://comments.youku.com/comments/~ajax/vpcommentContent.html?__ap=%7B%22id%22%3A%22XMzcyOTQ0MTY4%22%2C%22sid%22%3A355016202%2C%22page%22%3A2%2C%22last_modify%22%3A1333080602%7D&__ai=&__callback=displayComments");
    Pattern pp = Pattern.compile("<p id=\\\"content_.*?>(.*?)<");
    try {
    System.out.println(client.executeMethod(get));
    System.out.println(client.executeMethod(get2));
    String rsult=get2.getResponseBodyAsString();
    rsult=rsult.replaceAll("\\\\\"","\"");
    Matcher mm = pp.matcher(rsult);
    while(mm.find()){
    String yy=mm.group(1).replaceAll("\\\\","%");
    System.out.println(unescape(yy));
    }
    } catch (HttpException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println();
    }
    }花了一大晚上的时间才弄出了这个类....纠结的java编码
      

  13.   

    这里的换一下String yy=mm.group(1).replaceAll("\\\\u","%u");
      

  14.   


    谢谢你哦,昨天没来看!还有一点问题再麻烦您下,为什么控制台输出的\,在replaceAll里面的需要\\\\来转义。还有具体的unescape 函数能讲解下嘛。