为什么在Jsoup中使用迭代器检索Youtube元素时会引发异常?

时间:2014-12-02 14:49:47

标签: java jsoup

我检索了Youtube注释并使用Jsoup写入文件 mycode的:

Document docComments;
BufferedWriter bw1 = new BufferedWriter(new FileWriter(
                "D:\\chuchutvlink1.txt"));

    docComments = Jsoup
                .connect(
                        "https://apis.google.com/u/0/wm/4/_/widget/render/comments?usegapi=1&first_party_property=YOUTUBE&href=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdWO9uP_VJV8%26list%3DTLDVdYZggWP-Q&owner_id=BnZ16ahKA2DZ_T5W0FPUXg&query=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdWO9uP_VJV8&stream_id=UCBnZ16ahKA2DZ_T5W0FPUXg&substream_id=dWO9uP_VJV8&view_type=FILTERED&width=826&youtube_video_acl=PUBLIC&viewer_id=UCI7Gw-Kd1PkeBDQ7C33kN9A&hl=en_US&origin=https%3A%2F%2Fwww.youtube.com&search=%3Fv%3DdWO9uP_VJV8%26list%3DTLDVdYZggWP-Q&hash=&gsrc=1p&jsh=m%3B%2F_%2Fscs%2Fabc-static%2F_%2Fjs%2Fk%3Dgapi.gapi.en.T4EayPRcrOA.O%2Fm%3D__features__%2Frt%3Dj%2Fd%3D1%2Frs%3DAItRSTOp4ORGjvVzLjTlu0PIOZx2FtcWuA#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh%2Confirsttimeplusonepromo%2Conthumbsup%2Contimestampclicked%2Conshareboxopen%2Conready%2Conallcommentsclicked&id=I0_1417526304794&parent=https%3A%2F%2Fwww.youtube.com&pfname=&rpctoken=63150800")
                        .get();
 Elements userComments = docComments.select("h3[class=Mpa]");
        Elements userName = userComments.select("span[class=Ub gna]");
        Elements userCommentsTime = docComments.select("span[class=PL]");
        Elements userLink = userComments.select("a");
        Elements userContent = docComments.select("div[class=Ct]");
        Elements usercommentsLike = docComments
                .select("span[class=uPc bmd]");
        Elements userReply = docComments.select("div[class=fR]");

        Iterator<Element> itrUserName = userName.iterator();
        Iterator<Element> itrCommentsTime = userCommentsTime.iterator();
        Iterator<Element> itrUserLink = userLink.iterator();
        Iterator<Element> itrUserContent = userContent.iterator();
        Iterator<Element> itrUserCommentsLike = usercommentsLike.iterator();
        Iterator<Element> itrViewReply = userReply.iterator();

        while (itrUserName.hasNext() || itrCommentsTime.hasNext()
                || itrUserLink.hasNext() || itrUserContent.hasNext()
                || itrUserCommentsLike.hasNext() || itrViewReply.hasNext()) {

            // System.out.println("count"+count);
            System.out.print("Name Of The User:  "
                    + itrUserName.next().text() + "   ");
            System.out.println(itrCommentsTime.next().text());
            System.out.println("userCommandsLink:  "
                    + itrUserLink.next().attr("href"));
            System.out.println("Content :" + itrUserContent.next().text());
            System.out.println("Like : "
                    + itrUserCommentsLike.next().text());
            System.out.println(itrViewReply.next().text() + "\n");
              bwriter.write("Name Of The User:  " +
              itrUserName.next().text() + "   "); 
              bwriter.newLine();
              bwriter.write(itrCommentsTime.next().text());
              bwriter.newLine(); bwriter.write("userCommandsLink:  " +
              itrUserLink.next().attr("href")); bwriter.newLine();
              bwriter.write("Content :" + itrUserContent.next().text());
              bwriter.newLine(); bwriter.write("Like : " +
              itrUserCommentsLike.next().text());

        }
         bwriter.close();
    } catch (Exception to) {
        to.printStackTrace();
    }

我运行此代码时发生异常

java.util.NoSuchElementException
    at java.util.AbstractList$Itr.next(Unknown Source)
    at Link1.main(Link1.java:116)

0 个答案:

没有答案