Java Scraper- Google News

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONObject; public class GoogleNewsSearch { private static final String API_KEY = "YOUR_GOOGLE_NEWS_API_KEY"; public static void main(String[] args) { SwingUtilities.invokeLater(() -> createAndShowGUI()); } private static void createAndShowGUI() { JFrame frame = new JFrame("Google News Search"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); JLabel label = new JLabel("Enter search query:"); panel.add(label, BorderLayout.NORTH); JTextField textField = new JTextField(); panel.add(textField, BorderLayout.CENTER); JButton button = new JButton("Search"); panel.add(button, BorderLayout.SOUTH); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String query = textField.getText(); if (query.isEmpty()) { JOptionPane.showMessageDialog(frame, "Please enter a search query."); return; } String url = buildUrl(query); try { String response = sendRequest(url); displayResults(response); } catch (IOException ex) { JOptionPane.showMessageDialog(frame, "An error occurred while fetching news articles."); } } }); frame.add(panel); frame.setVisible(true); } private static String buildUrl(String query) { String encodedQuery = query.replaceAll(" ", "%20"); return "https://newsapi.org/v2/top-headlines?q=" + encodedQuery + "&apiKey=" + API_KEY; } private static String sendRequest(String urlString) throws IOException { StringBuilder response = new StringBuilder(); URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); } return response.toString(); } private static void displayResults(String response) { JSONObject jsonObject = new JSONObject(response); if (jsonObject.has("articles")) { JSONArray articlesArray = jsonObject.getJSONArray("articles"); StringBuilder results = new StringBuilder(); for (int i = 0; i < articlesArray.length(); i++) { JSONObject article = articlesArray.getJSONObject(i); String title = article.getString("title"); String url = article.getString("url"); results.append(title).append("\n").append(url).append("\n\n"); } JOptionPane.showMessageDialog(null, results.toString(), "Top News Articles", JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null, "No articles found.", "Top News Articles", JOptionPane.INFORMATION_MESSAGE); } } }