소스 코드를 검색하고 파싱하기 위해 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
}
}
클래스 및 메소드 주석을 javadoc 주석으로 변환하는 것을 고려 했습니까? 그것은 당신의 코드를 클래스/메소드 앞에 줄 주석으로 두는 것보다 훨씬 낫게 만들 것입니다. – AJMansfield
당신은 다음 버튼이 무엇을하는지, 어떤 URL을 호출하는지, 그리고 다음 페이지를 검색하기 위해 어떤 파라미터를 전달해야 하는지를 알아야합니다. 정보를 얻을 수 있으면 설정됩니다. –
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