Executing javascript in java - Opening a URL and getting links

后端 未结 5 1925
醉话见心
醉话见心 2020-12-19 18:57
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;

public class Main {

    public static void main(String[] args         


        
相关标签:
5条回答
  • 2020-12-19 19:27

    try this:

    import java.net.*;  
    import java.io.*;  
      public class URLConnectionReader {  
      public static void main(String[] args) throws Exception {  
            URL yahoo = new URL("http://www.yahoo.com/");  
            URLConnection yc = yahoo.openConnection();  
            BufferedReader in = new BufferedReader(  
                 new InputStreamReader(  
                 yc.getInputStream()));  
           String inputLine;  
           while ((inputLine = in.readLine()) != null)   
                 System.out.println(inputLine);// or save to some StringBuilder like this:   sb.append(inputLine); then pass the sb.toString() to the method that gets links out of it - > see getLinks below  
            in.close();  
           }  
      }  
    
    
    
    private static final String CLOSING_QUOTE   = "\"";
    private static final String HREF_PREFIX     = "href=\"";
    private static final String HTTP_PREFIX     = "http://";
    
    
    
    public static Set<String> getLinks(String page) {
        Set<String> links = new HashSet<String>();
        String[] rawLinks = StringUtils.splitByWholeSeparator(page, HREF_PREFIX);
        for (String str : rawLinks) {
            if(str.startsWith(HTTP_PREFIX)) {
                links.add(StringUtils.substringBefore(str, CLOSING_QUOTE));
            }
        }
        return links;
    }
    
    0 讨论(0)
  • 2020-12-19 19:30

    you can embed Env.js in Rhino to get this kind of functionality

    0 讨论(0)
  • 2020-12-19 19:31

    According to the documentation:

    The window object represents an open window in a browser.

    Since you are not executing your script in a browser, the window object is not defined.

    You can read the URL using the URL/URLConnecion classes and feed it to the ScriptEngine. There is a tutorial here.

    0 讨论(0)
  • 2020-12-19 19:41

    In javascript window means browser window. So when you are trying to execute this js from Java, it is unable to find browser window and you are getting error. You can use URL class in Java to get the content of the url.

    0 讨论(0)
  • 2020-12-19 19:46

    you can use HtmlUnit is java API, i think it can help you to access the executed js content, as a simple html.

    WebClient webClient = new WebClient();
    HtmlPage myPage = (HtmlPage) webClient.getPage(new URL("YourURL"));
    System.out.println(myPage.getVisibleText());
    
    0 讨论(0)
提交回复
热议问题