Reading data from a website with HTTP GET and regu

2019-09-21 01:58发布

问题:

I have a PHP script which prints something like "online" "offline" and a specific number which change in a specific time. I found an example for a http get request, and now I need something like Regex I think, right? :o Does anyone have a tutorial or can give me a simple example?

The code of the request:

String urlString;
urlString = "hear is my url^^";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream IS = conn.getInputStream();

The Inputstream IS is the "sourcecode" of the website, right?

回答1:

You are correct. The IS contains the HTML source. You should consider using a HTML parsing library to extract the data. Regex is going to be brittle. There are many html parsers out there. Look at the below SO post

HTML/XML Parser for Java



回答2:

Actually you dont need to use URLConnection to get the data from PHP script. You can use Jsoup library to get data from PHP script and using this library you can parse the data as you want. You will get it from here

Jsoup API

and here is how to use it.

Cookbook for Jsoup