Use tools:
httpClient+jsoup
Brief introduction: HttpClient is a sub-project under Apache Jakarta Common. It can be used to provide an efficient, up-to-date and feature-rich client programming toolkit supporting HTTP protocol, and it supports the latest version and recommendations of HTTP protocol. (From 360 encyclopedias, Wikipedia does not have this term? jsoup is a Java HTML parser that can directly parse a URL address and HTML text content. It provides a very labor-saving API for extracting and manipulating data through DOM, CSS and jQuery-like operations. (from 360 encyclopedias)
How to use httpClient:
(For more information, please refer to the blog: http://blog.csdn.net/wangpeng047/article/details/19624529/ Or httpClient: http://hc.apache.org/httpcomponents-client-5.0.x/index.html)
Using HttpClient to send requests and receive responses is very simple. Generally, the following steps are needed.
1. Create the HttpClient object.
2. Create an instance of the request method and specify the request URL. If you need to send a GET request, create the HttpGet object; if you need to send a POST request, create the HttpPost object.
3. If you need to send request parameters, you can call HttpGet and HttpPost common setParams(HetpParams params) method to add request parameters; for HttpPost objects, you can also call setEntity(HttpEntity entity) method to set request parameters.
4. Call the execute(HttpUriRequest request) of the HttpClient object to send the request, which returns a HttpResponse.
5. Calling the getAllHeaders(), getHeaders(String name) and other methods of HttpResponse can obtain the server's response headers; calling the getEntity() method of HttpResponse can obtain the HttpEntity object, which wraps the server's response content. The program can get the response content of the server through this object.
6. Release the connection. Whether or not the execution method succeeds, the connection must be released
How to use jsoup:
Please refer to:
Jsoup development guide, jsoup Chinese usage manual, jsoup Chinese documents:
http://www.open-open.com/jsoup/
Jsoup Parsing Html Tutorial | xdemo.org
http://www.xdemo.org/jsoup-html-parse/
Jsoup parses HTML instances and document methods detailing java script home http://www.jb51.net/article/43485.htm
jar package download:
httpclient jar package download address: Apache HttpComponents - HttpComponents Downloads http://hc.apache.org/downloads.cgi
jsoup jar package download address: Download and install jsoup https://jsoup.org/download
Effect:
Code:
Almanc.java
package permanentcalendar;
/**
* @author Kou Shuang
* @Description: ${TODO}(Define the variables and getter and setter methods used
*/
public class Almanac {
private String solar;
private String lunar;
private String chineseAra;
private String should;// Store what you need to do today
private String avoid;// Store what you don't want to do today
private String festival;// Store festivals
public String getSolar() {
return solar;
}
public void setSolar(String date) {
this.solar = date;
}
public String getLunar() {
return lunar;
}
public void setLunar(String lunar) {
this.lunar = lunar;
}
public String getChineseAra() {
return chineseAra;
}
public void setChineseAra(String chineseAra) {
this.chineseAra = chineseAra;
}
public String getAvoid() {
return avoid;
}
public void setAvoid(String avoid) {
this.avoid = avoid;
}
public String getShould() {
return should;
}
public void setShould(String should) {
this.should = should;
}
public String getFestival() {
return festival;
}
public void setFestival(String festival) {
this.festival = festival;
}
public Almanac(String solar, String lunar, String chineseAra, String should, String avoid,String festival) {
this.solar = solar;
this.lunar = lunar;
this.chineseAra = chineseAra;
this.should = should;
this.avoid = avoid;
this.festival=festival;
}
}
AlmanacUtil.java
package permanentcalendar;
import java.io.Closeable;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* @author Kou Shuang
* @Description: ${TODO}(Getting Web Content)
*/
public class AlmanacUtil {
/**
* Singleton Tool Class
*/
private AlmanacUtil() {
}
public static Almanac getAlmanac() {
String url = "http://tools.2345.com/rili.htm";
String html = pickData(url);
Almanac almanac = analyzeHTMLByString(html);
return almanac;
}
/*
* Crawling Web Page Information
*/
private static String pickData(String url) {
HttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
try {
// Getting response entities
HttpEntity entity = response.getEntity();
// Print response status
if (entity != null) {
return EntityUtils.toString(entity);
}
} finally {
((Closeable) response).close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the connection and release resources
try {
((Closeable) httpclient).close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/*
* Using jsoup to parse web page information
*/
private static Almanac analyzeHTMLByString(String html) {
String solarDate, lunarDate, chineseAra, should, avoid, festival = " ";
Document document = Jsoup.parse(html);
// Obtaining Calendar Time
Element solar = document.getElementById("info_all");
solarDate = solar.text().toString();
// Obtaining Lunar Calendar Time
Element eLunarDate = document.getElementById("info_nong");
lunarDate = eLunarDate.child(0).html().substring(1, 3) + " " + eLunarDate.html().substring(11);
//Acquisition of the chronology of Tiangan and Dizhi Branches
Element eChineseAra = document.getElementById("info_chang");
chineseAra = eChineseAra.text().toString();
// Appropriate access
should = getSuggestion(document, "yi");
// Avoid
avoid = getSuggestion(document, "ji");
// festival
festival = getSuggestion(document, "festival");
Almanac almanac = new Almanac(solarDate, lunarDate, chineseAra, should, avoid, festival);
return almanac;
}
/*
* Obtain taboos/preferences
*/
private static String getSuggestion(Document doc, String id) {
Element element = doc.getElementById(id);
Elements elements = element.getElementsByTag("a");
StringBuffer sb = new StringBuffer();
for (Element e : elements) {
sb.append(e.text() + " ");
}
return sb.toString();
}
}
Face.java
package permanentcalendar;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class Face {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Face window = new Face();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Face() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("Personal calendar");
frame.setBounds(100, 100, 516, 367);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//Gregorian calendar time
JLabel Calendar = new JLabel("\u516C\u5386\u65F6\u95F4\uFF1A");
Calendar.setFont(new Font("STLiti", Font.PLAIN, 19));
Calendar.setBounds(10, 39, 95, 19);
frame.getContentPane().add(Calendar);
Almanac almanac=AlmanacUtil.getAlmanac();
//Lunar calendar time
JLabel lunar = new JLabel("\u519C\u5386\u65F6\u95F4\uFF1A");
lunar.setFont(new Font("STLiti", Font.PLAIN, 19));
lunar.setBounds(10, 94, 95, 19);
frame.getContentPane().add(lunar);
//Heavenly stems and Earthly Branches
JLabel ChineseEra = new JLabel("\u5929\u5E72\u5730\u652F\uFF1A");
ChineseEra.setFont(new Font("STLiti", Font.PLAIN, 19));
ChineseEra.setBounds(10, 140, 95, 19);
frame.getContentPane().add(ChineseEra);
//should
JLabel should = new JLabel("\u5B9C\uFF1A");
should.setFont(new Font("STLiti", Font.PLAIN, 19));
should.setBounds(10, 181, 95, 19);
frame.getContentPane().add(should);
//festival
JLabel festival = new JLabel("\u8282\u65E5\uFF1A");
festival.setFont(new Font("STLiti", Font.PLAIN, 19));
festival.setBounds(10, 274, 95, 19);
frame.getContentPane().add(festival);
//Avoid
JLabel avoid = new JLabel("\u5FCC\uFF1A");
avoid.setFont(new Font("STLiti", Font.BOLD, 19));
avoid.setBounds(10, 226, 95, 19);
frame.getContentPane().add(avoid);
//Gregorian calendar time
JTextArea CalendarContent = new JTextArea();
CalendarContent.setBounds(134, 39, 315, 24);
frame.getContentPane().add(CalendarContent);
CalendarContent.setEnabled(false);
CalendarContent.setText(almanac.getSolar());
CalendarContent.setFont(new Font("STLiti",Font.BOLD,19));
//Lunar calendar time
JTextArea lunarcontent = new JTextArea();
lunarcontent.setBounds(134, 89, 315, 24);
frame.getContentPane().add(lunarcontent);
lunarcontent.setEnabled(false);
lunarcontent.setText(almanac.getLunar());
lunarcontent.setFont(new Font("STLiti",Font.BOLD,19));
//Heavenly stems and Earthly Branches
JTextArea ChineseeraContent = new JTextArea();
ChineseeraContent.setBounds(134, 135, 315, 24);
frame.getContentPane().add(ChineseeraContent);
ChineseeraContent.setEnabled(false);
ChineseeraContent.setText(almanac.getChineseAra());
ChineseeraContent.setFont(new Font("STLiti",Font.BOLD,19));
//should
JTextArea shouldContent = new JTextArea();
shouldContent.setBounds(134, 181, 315, 24);
frame.getContentPane().add(shouldContent);
shouldContent.setEnabled(false);
shouldContent.setText(almanac.getShould());
shouldContent.setFont(new Font("STLiti",Font.BOLD,19));
//Avoid
JTextArea avoidContent = new JTextArea();
avoidContent.setBounds(134, 226, 315, 24);
frame.getContentPane().add(avoidContent);
avoidContent.setEnabled(false);
avoidContent.setText(almanac.getAvoid());
avoidContent.setFont(new Font("STLiti",Font.BOLD,19));
//festival
JTextArea festivaContent = new JTextArea();
festivaContent.setBounds(134, 274, 315, 24);
frame.getContentPane().add(festivaContent);
festivaContent.setEnabled(false);
festivaContent.setText(almanac.getFestival());
festivaContent.setFont(new Font("STLiti",Font.BOLD,19));
}
}