如何读取其他html后加载的html?

时间:2013-04-05 00:01:34

标签: java regex bufferedreader

好的,抱歉这个问题很糟糕。很难提出一个问题:P

如果你将一个像Thought这样的词放入dictionary.com或任何不规则动词,那么发音[thawt]会在其余的html之后加载。

如果你检查我的代码,你会明白我的意思。而不是[thawt]它加载[thingk] ..

package OEpr;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

public class OEpr {

    private JFrame frame;
    private JTextField txtWord;
    private JTextField textpron;

    /**
     * Launch the application.
     */

    private static String wordurl;
    private static String line;
    private int linenum;
    private static int findingfirst = 0;
    private static String pron;
    StringSelection testcopy;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OEpr window = new OEpr();
                    window.frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the application.
     */
    public OEpr() {
        initialize();

        setvars();

    }

    public void setvars() {
        OEpr.wordurl = "http://dictionary.reference.com/browse/"
                + txtWord.getText();

    }

    public void geturl(String[] args) throws Exception {

        // Set URL

        URL url = new URL(wordurl);
        URLConnection spoof = url.openConnection();
        // Spoof the connection so we look like a web browser
        spoof.setRequestProperty("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                spoof.getInputStream()));
        String uline = "";

        // Loop through every line in the source
        while ((uline = in.readLine()) != null) {

            System.out.println(uline);
            line = uline;

            linecheck();
        }
        // System.out.println(OEpr.line);
    }

    private void linecheck() {
        Pattern pattern = Pattern.compile("<!-- google_ad_section_start -->");
        Pattern pattern2 = Pattern.compile("\\[.*?]");
        Matcher matcher = pattern.matcher(OEpr.line);
        if (matcher.find()) {
            findingfirst++;
            // if(findingfirst==2){

            String noHTML = line.replaceAll("\\<.*?>", "");
            Matcher matcher2 = pattern2.matcher(noHTML);
            if (matcher2.find()) {
                pron = noHTML.replaceAll("(^.*(?=\\[))|((?<=\\]).*$)", "");
                // System.out.println(pron);
                textpron.setText(pron);
            }

            // }
            // System.out.println(matcher.group(0)); //prints /{item}/
        } else {
            // System.out.println("Match not found");
        }
    }

    public void setClipboardContents(String aString) {

        testcopy = new StringSelection(pron);

        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(testcopy, null);

    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setAlwaysOnTop(true);
        frame.setResizable(false);
        frame.setSize(210, 100);
        JButton btnGo = new JButton("Go!");
        btnGo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                // Execute when button is pressed
                try {
                    setvars();
                    OEpr.findingfirst = 0;
                    geturl(null);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        JButton btnCopy = new JButton("Copy!");
        btnCopy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                // Execute when button is pressed
                try {
                    setClipboardContents(null);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        txtWord = new JTextField();
        txtWord.setText("thought");
        txtWord.setColumns(10);

        textpron = new JTextField();
        textpron.setColumns(10);
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout
                .setHorizontalGroup(groupLayout
                        .createParallelGroup(Alignment.LEADING)
                        .addGroup(
                                Alignment.TRAILING,
                                groupLayout
                                        .createSequentialGroup()
                                        .addGroup(
                                                groupLayout
                                                        .createParallelGroup(
                                                                Alignment.LEADING)
                                                        .addGroup(
                                                                groupLayout
                                                                        .createSequentialGroup()
                                                                        .addContainerGap(
                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(
                                                                                txtWord,
                                                                                GroupLayout.PREFERRED_SIZE,
                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                GroupLayout.PREFERRED_SIZE)
                                                                        .addPreferredGap(
                                                                                ComponentPlacement.RELATED))
                                                        .addGroup(
                                                                groupLayout
                                                                        .createSequentialGroup()
                                                                        .addContainerGap()
                                                                        .addComponent(
                                                                                textpron,
                                                                                GroupLayout.PREFERRED_SIZE,
                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                GroupLayout.PREFERRED_SIZE)
                                                                        .addPreferredGap(
                                                                                ComponentPlacement.RELATED)))
                                        .addGroup(
                                                groupLayout
                                                        .createParallelGroup(
                                                                Alignment.LEADING)
                                                        .addComponent(btnGo)
                                                        .addComponent(btnCopy))
                                        .addContainerGap()));
        groupLayout
                .setVerticalGroup(groupLayout
                        .createParallelGroup(Alignment.LEADING)
                        .addGroup(
                                groupLayout
                                        .createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                groupLayout
                                                        .createParallelGroup(
                                                                Alignment.BASELINE)
                                                        .addComponent(btnGo)
                                                        .addComponent(
                                                                txtWord,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(
                                                ComponentPlacement.RELATED)
                                        .addGroup(
                                                groupLayout
                                                        .createParallelGroup(
                                                                Alignment.BASELINE)
                                                        .addComponent(btnCopy)
                                                        .addComponent(
                                                                textpron,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addContainerGap(
                                                GroupLayout.DEFAULT_SIZE,
                                                Short.MAX_VALUE)));
        frame.getContentPane().setLayout(groupLayout);
    }
}

0 个答案:

没有答案