How Java gets the dynamic content of a Web page and parses the page content

Source: Internet
Author: User

Notes

Get a dynamic content reference for a Web page

Https://pastebin.com/raw/FePkm2kp

Maven:

<!--access to Web source, including dynamic content--
<dependency>
<groupId>htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>1.14</version>
</dependency>

Realize:
WebClient WebClient = new WebClient ()//don ' t disable it if you want JS enabled;webclient.setcssenabled (false); WebClient. Setappletenabled (false); webclient.setjavascriptenabled (false); webclient.settimeout (7000); WebRequest request = new WebRequest (New URL ("http://portal.hud.gov/portal/page/portal/HUD/"));// Request.setadditionalheader ("Authorization", "Basic bg9hbnnkzxy6bg9abnnkm3y="); Page page = webclient.getpage (request); String ContentType = Page.getwebresponse (). getContentType (); int statusCode = Page.getwebresponse (). Getstatuscode (); String StatusMessage = Page.getwebresponse (). Getstatusmessage (); Long loadtime = Page.getwebresponse (). GetLoadTime (); System.out.println (StatusCode); System.out.println (StatusMessage); System.out.println (ContentType); System.out.println (Loadtime); System.out.println (Page.getwebresponse (). getcontentasstring ());

Https://stackoverflow.com/questions/42446990/parse-html-table-to-json-using-jsoup-in-java

Maven:

<!--convert HTML to map-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>

Realize:
Public String Tabletojson(StringSource) Throws Jsonexception { DocumentDoc= Jsoup.Parse(Source); JsonobjectJsonparentobject= New Jsonobject(); Jsonarray list = new Jsonarray (); For (ElementTable:Doc.Select("Table")) { For (ElementRow:Table.Select("TR")) { JsonobjectJsonobject= New Jsonobject(); ElementsTds=Row.Select("TD"); String Name =Tds.Get(0).Text(); String Group =Tds.Get(1).Text(); String Code =Tds.Get(2).Text(); String Lesson =Tds.Get(3).Text(); String Day1 =Tds.Get(4).Text(); String Day2 =Tds.Get(5).Text(); String Day3=Tds.Get(6).Text();Jsonobject.Put("Group", Group);Jsonobject.Put("Code", Code);Jsonobject.Put("Lesson", Lesson);Jsonobject.Put("Day1", Day1);Jsonobject.Put ( "Day2" , day2  Jsonobject. ( "Day3" , day3 Jsonparentobject. (name,jsonobject) ;  } } return  Jsonparentobject.               /span>               

How Java Gets the dynamic content of a Web page and parses the page content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.