自助网站搭建系统,做网站接电话一般要会什么,虹口建设机械网站制作,为女朋友做的表白网站JSOUP指的是前端爬虫框架#xff0c;对HTML网页的一系列操作包括信息的获取内容的修改等。
jsoup简单应用
1.三种加载HTML的方法 Testpublic void test1() throws IOException {//从URL加载HTMLDocument document Jsoup.connect(http://www.guge.com).get();St…JSOUP指的是前端爬虫框架对HTML网页的一系列操作包括信息的获取内容的修改等。
jsoup简单应用
1.三种加载HTML的方法 Testpublic void test1() throws IOException {//从URL加载HTMLDocument document Jsoup.connect(http://www.guge.com).get();String title document.title();//获取html中的标题System.out.println(title :title);//从字符串加载HTMLString html htmlheadtitleFirst parse/title/head bodypParsed HTML into a doc./p/body/html;Document doc Jsoup.parse(html);title doc.title();System.out.println(title :title);//从文件加载HTMLdoc Jsoup.parse(new File(d:\\file\\html\\index.html),utf-8);title doc.title();System.out.println(title :title);}2.获取html中的headbody,url等信息 Testpublic void test2() throws IOException {Document document Jsoup.connect(http://www.guge.com).get();String title document.title();System.out.println(title :title);//获取html中的headSystem.out.println(document.head());//获取html中的body//System.out.println(document.body());//获取HTML页面中的所有链接Elements links document.select(a[href]);for (Element link : links){System.out.println(link : link.attr(href));System.out.println(text : link.text());}}3.获取URL的地址信息 Testpublic void test3() throws IOException {Document document Jsoup.connect(https://passport.lagou.com).get();System.out.println(document.head());//获取URL的元信息String description document.select(meta[namedescription]).get(0).attr(content);System.out.println(Meta description : description);String keywords document.select(meta[namekeywords]).first().attr(content);System.out.println(Meta keyword : keywords);}4.根据class名称获取表单 Testpublic void test4() throws IOException {Document document Jsoup.connect(https://passport.lagou.com/login/login.html?signature8ECBCDF2B86061432B425A0B94FC863Bservicehttps%253A%252F%252Fwww.lagou.com%252FactionloginserviceIdlagouts1547711303033).get();//获取拉勾网登入页面的body//System.out.println(document.body());//根据class名称获取表单Elements formElement document.getElementsByClass(form_body);System.out.println(formElement.html());//获取URL的元信息for (Element inputElement : formElement) {String placeholder inputElement.getElementsByTag(input).attr(placeholder);System.out.println(placeholder);}}5.提取并打印表单参数 Testpublic void test5() throws IOException {Document document Jsoup.parse(new File(d:\\file\\html\\index.html),utf-8);Element loginform document.getElementById(registerform);Elements inputElements loginform.getElementsByTag(input);for (Element inputElement : inputElements) {String key inputElement.attr(name);String value inputElement.attr(value);System.out.println(Param name: key -- Param value: value);}}6.设置元素的html内容 Testpublic void test6() throws IOException {Document document Jsoup.parse(new File(d:\\file\\html\\index.html),utf-8);System.out.println(document.body());// div iddiv1/divSystem.out.println(----------------);Element div document.select(div).first();div.html(pHello/p); // div iddiv1pHello/p/divdiv.prepend(pFiest/p); //div iddiv1pFiest/ppHello/p/divdiv.append(pLast/p); //div iddiv1pFiest/ppHello/ppLast/p/divSystem.out.println(document.body());System.out.println(------------------);System.out.println(div.text());System.out.println(-------------------);//对元素包裹一个外部HTML内容div.wrap(div id\div2\/div); //div iddiv2div iddiv1pFiest/ppHello/ppLast/p/divSystem.out.println(document.body());}7.设置元素的文本内容 Testpublic void test7() throws IOException {Document document Jsoup.parse(new File(d:\\file\\html\\index.html),utf-8);System.out.println(document.body());// div iddiv1/divSystem.out.println(-------------------);Element div document.select(div).first();div.text(7 8 ); // div iddiv17 gt; 8 /divdiv.prepend(Fiest ); //div iddiv1Fiest 7 gt; 8/divdiv.append(Last ); //div iddiv1Fiest 7 gt; 8 Last/divSystem.out.println(document.body());System.out.println(---------------);System.out.println(div.text());}了解更多关注我哟