2013-09-26 5 views
0

소스 코드를 검색하고 파싱하기 위해 Java로 코드를 작성하고 있습니다. 액세스하려는 웹 사이트는 http://cpdocket.cp.cuyahogacounty.us/SheriffSearch/results.aspx?q=searchType%3dSaleDate%26searchString%3d9%2f30%2f2013%26foreclosureType%3d%27NONT%27%2c+%27PAR%27%2c+%27COMM%27%2c+%27TXLN%27입니다.자바 코드에서 다음 버튼 클릭을 구현하는 방법은 무엇입니까?

총 11 개의 페이지가 있지만 소스 코드는 해당 페이지에만 있습니다. 다음 페이지의 소스 코드에 액세스하려면 다음 버튼을 클릭하고 새 소스 코드를 보려면 페이지를 다시로드해야합니다. 내 코드가 소스 코드의 모든 다른 페이지를 검색하도록하려면이 아이디어를 구현해야한다.

필자는 PhantomJS 또는 CasperJS를 사용하여이 작업을 수행하는 방법에 대해 읽었지만이를 구현하는 방법을 모르겠습니다. 다음과 같이

내 코드는 다음과 같습니다

여기
// Scraper class takes an input of a string, and returns the source code of the of the website. Also picks out the needed data 
public class Scraper { 

    private static String url; // the input website to be scraped 

    public static String sourcetext; //The source code that has been scraped 


    //constructor which allows for the input of a URL 
    public Scraper(String url) { 
    this.url = url; 
    } 

    //scrapeWebsite runs the method to scrape the input URL and returns a string to be parsed. 
    public static void scrapeWebsite() throws IOException { 

    URL urlconnect = new URL(url); //creates the url from the variable 
    URLConnection connection = urlconnect.openConnection(); // connects to the created URL 
    BufferedReader in = new BufferedReader(new InputStreamReader( 
                   connection.getInputStream(), "UTF-8")); // annonymous class to stream the website 
    String inputLine; //creates a new variable of string 
    StringBuilder sourcecode = new StringBuilder(); // creates a stringbuilder which contains the sourcecode 

    //loop appends to the string builder as long as there is information 
    while ((inputLine = in.readLine()) != null) 
     sourcecode.append(inputLine);// appends the source code to the sting 
    in.close(); 
    sourcetext = sourcecode.toString(); // Takes the text in stringbuilder and converts it to a string 
    sourcetext = sourcetext.replace('"','*'); //deletes the quotes(") so it can be parsed 
    } 

    //This method parses through the data and adds the necesary information to a specified CSV file 
    public static void getPlaintiff() throws IOException { 

    PrintWriter docketFile = new PrintWriter("tester.csv", "UTF-8"); // creates the csv file. (name must be changed, override deletes file) 

    int i = 0; 

    //While loop runs through all the data in the source code. There is (14) entries per page. 
    while(i<14) { 
     String plaintiffAtty = "PlaintiffAtty_"+i+"*>"; //creates the search string for the plaintiffatty 
     Pattern plaintiffPattern = Pattern.compile("(?<="+Pattern.quote(plaintiffAtty)+").*?(?=</span>)");//creates the pattern for the atty 
     Matcher plaintiffMatcher = plaintiffPattern.matcher(sourcetext); // looks for a match for the atty 

     while (plaintiffMatcher.find()) { 
     docketFile.write(plaintiffMatcher.group().toString()+", "); //writes the found atty to the file 
     } 

     String appraisedValue = "Appraised_"+i+"*>"; //creats the search string for the appraised value 
     Pattern appraisedPattern = Pattern.compile("(?<="+Pattern.quote(appraisedValue)+").*?(?=</span>)");//creates the parren for the value 
     Matcher appraisedMatcher = appraisedPattern.matcher(sourcetext); //looks for a match to the apreaised value 

     while (appraisedMatcher.find()) { 
     docketFile.write(appraisedMatcher.group().toString()+"\n"); //writes the found value to the file 

     } 
     i++; 
    } 
    docketFile.close(); //closes the file 
    } 
} 
+0

클래스 및 메소드 주석을 javadoc 주석으로 변환하는 것을 고려 했습니까? 그것은 당신의 코드를 클래스/메소드 앞에 줄 주석으로 두는 것보다 훨씬 낫게 만들 것입니다. – AJMansfield

+2

당신은 다음 버튼이 무엇을하는지, 어떤 URL을 호출하는지, 그리고 다음 페이지를 검색하기 위해 어떤 파라미터를 전달해야 하는지를 알아야합니다. 정보를 얻을 수 있으면 설정됩니다. –

+0

ns47731 님의 댓글에 추가. 필자는 [Live Http Headers] (https://addons.mozilla.org/En-us/firefox/addon/live-http-headers/)라는 Firefox 플러그인을 살펴볼 것입니다. 이 프로그램을 통해 다음 url을 호출해야하는지 확인할 수 있습니다. progrommatically (올바른 자바 스크립트 함수를 호출하는 경우에도) 모든 것을해야한다면, [Selenium] (http://docs.seleniumhq.org/)을 살펴보고 javascript 등을 호출하는 방법을 살펴보십시오. 행운을 빌어 요. 마지막으로 HTTP 요청을 만들기 위해 HttpClient를 살펴보십시오. URLConnection을 사용하면 기껏해야 배신 행위가됩니다. – hooknc

답변

0

새, majorly, 포맷 재 스타일, 그리고 개선 된 코드가; 이제는 실제로 이해할 수있게되었으므로 자신의 문제를 해결할 수 있습니다. (그들은 1.7에 추가 된 때문에,하지만, 이전 당신이 자바 1.6를 사용하는 경우 시도 -과 - 자원 일부를 되돌 리거나 할 수 있습니다.) 새로운 버그가 잠재력을 가질 수

/** 
* This class contains methods for is for picking 
* out needed data from the source of a website. 
*/ 
public class Scraper { 

    /** 
    * This method scrapes the input URL. 
    * @return A string containing the data from the webpage. 
    * @throws IOException if there was a problem with accessing the website. 
    */ 
    public static String scrapeWebsite(String url) throws IOException { 

     String inputLine; 
     StringBuilder sourcetext = new StringBuilder(); 

     URL urlconnect = new URL(url); 
     URLConnection connection = urlconnect.openConnection(); 

     try(BufferedReader in = new BufferedReader(
       new InputStreamReader(connection.getInputStream(), "UTF-8"))){ 

      while ((inputLine = in.readLine()) != null) 
       sourcetext.append(inputLine); 
     } 
     return sourceText.toString().replace('"','*'); 
    } 

    /** 
    * This method parses through the data and adds the necesary information to 
    * a specified .CSV file. 
    * @param source The datasource, for example that returned by 
    *    {@link scrapeWebsite()}. 
    * @param targetFile The file path for the destination .csv file. 
    * @throws IOException if there was a problem with accessing the file. 
    */ 
    public static void getPlaintiff(CharSequence source, String targetFile) 
      throws IOException{ 

     try(PrintWriter docketFile = new PrintWriter("tester.csv", "UTF-8")){ 

      for(int i = 0; i < 14; i++) { 
       Matcher plaintiffMatcher = Pattern.compile(
         "(?<=PlaintiffAtty_" + i + "\\*>).*?(?=</span>)") 
         .matcher(source); 

       while (plaintiffMatcher.find()) 
        docketFile.println(plaintiffMatcher.group()); 

       Matcher appraisedMatcher = Pattern.compile(
         "(?<=Appraised_" + i + "\\*>).*?(?=</span>)") 
         .matcher(source); 

       while (appraisedMatcher.find()) 
        docketFile.println(appraisedMatcher.group()); 
      } 
     } 
    } 
} 

는 (주의 도입 된, 큰 문제가되지 않습니다.)

EDIT : 정규식 생성을 위해 인덱스가 필요하기 때문에 matcher 생성이 실제로 루프 내부에서 수행되어야한다는 것을 알았습니다. 또한 docketWriter.write을 훨씬 간단한 docketWriter.println 상태로 대체했습니다.

관련 문제