2013-10-12 2 views
4

현재 웹 페이지를 구문 분석하고 분석하는 데 응용 프로그램에서 jsoup를 사용하고 있습니다. 그러나 robot.txt 규칙을 준수하고 있는지 확인하고 싶습니다. 허용되는 페이지java를 사용하여 robot.txt 구문 분석 및 URL 허용 여부 확인

저는 jsoup가이 용도로 만들어지지 않았으며 웹 스크래핑 및 파싱에 관한 모든 내용을 담고 있습니다. 그래서 도메인/사이트의 robot.txt를 읽어야하는 함수/모듈을 계획하고 내가 방문 할 URL이 허용되는지 여부를 확인했습니다.

나는 몇 가지 연구를했고 다음을 찾았습니다.하지만 이것들에 대해서는 확실하지 않습니다. robot.txt 구문 분석과 관련된 종류의 프로젝트가 당신의 생각과 아이디어를 공유한다면 큰 도움이 될 것입니다.

http://code.google.com/p/crowl/source/browse/trunk/Crow/src/org/crow/base/Robotstxt.java?r=12

+0

을? 그리고 robot.txt를 파싱하는 것이 Jsoup의 범위를 벗어나는 것 같습니다. Jsoup는 스스로 말한대로 웹 페이지를 구문 분석하기위한 것입니다. – Darwind

+0

고마워요. 예, 페이지 구문 분석에 jsoup를 사용하고 있습니다 ...하지만 요구 사항은 robots.txt에서만 허용되는 (제한되지 않음) URL을 구문 분석하는 것입니다. JS 유효성 검사는 JS 또는 JS가 최상 또는 유능하지 않은 것으로 보입니다. . 그래서 내가 알아야 할 것은 실제 파싱을하기 전에 robots.txt에서 어떻게 검증을 할 수 있는가입니다. –

+0

좋습니다. 좋습니다. jsoup를 사용하는 작은 프로젝트에서 일하고 싶었 기 때문에 직접 할 수도있었습니다. – alkis

답변

6

늦은 대답

https://code.google.com/p/crawler-commons/

http://sourceforge.net/projects/jrobotx/

단지 경우에 당신 - 또는 다른 사람은 - 아직이 작업을 수행 할 수있는 방법을 찾고 있습니다. 버전 0.2에서 https://code.google.com/p/crawler-commons/을 사용하고 있으며 잘 작동하는 것 같습니다. 여기 내가 사용하는 코드에서 간단한 예입니다 : 분명히 이것은 어떤 식 으로든 Jsoup 관련이 없습니다

String USER_AGENT = "WhateverBot"; 
String url = "http://www.....com/"; 
URL urlObj = new URL(url); 
String hostId = urlObj.getProtocol() + "://" + urlObj.getHost() 
       + (urlObj.getPort() > -1 ? ":" + urlObj.getPort() : ""); 
Map<String, BaseRobotRules> robotsTxtRules = new HashMap<String, BaseRobotRules>(); 
BaseRobotRules rules = robotsTxtRules.get(hostId); 
if (rules == null) { 
    HttpGet httpget = new HttpGet(hostId + "/robots.txt"); 
    HttpContext context = new BasicHttpContext(); 
    HttpResponse response = httpclient.execute(httpget, context); 
    if (response.getStatusLine() != null && response.getStatusLine().getStatusCode() == 404) { 
     rules = new SimpleRobotRules(RobotRulesMode.ALLOW_ALL); 
     // consume entity to deallocate connection 
     EntityUtils.consumeQuietly(response.getEntity()); 
    } else { 
     BufferedHttpEntity entity = new BufferedHttpEntity(response.getEntity()); 
     SimpleRobotRulesParser robotParser = new SimpleRobotRulesParser(); 
     rules = robotParser.parseContent(hostId, IOUtils.toByteArray(entity.getContent()), 
       "text/plain", USER_AGENT); 
    } 
    robotsTxtRules.put(hostId, rules); 
} 
boolean urlAllowed = rules.isAllowed(url); 

, 단지 주어진 URL은 특정 USER_AGENT에 대한 크롤링하도록 허용 여부를 체크한다. robots.txt를 가져 오려면 버전 4.2.1에서 Apache HttpClient를 사용하지만 java.net 항목으로도 바꿀 수 있습니다.

이 코드는 허용 또는 불허가 여부 만 확인하며 '크롤링 지연'과 같은 다른 robots.txt 기능은 고려하지 않습니다. 그러나 크롤러 - 커먼즈가이 기능을 제공하기 때문에 위의 코드에 쉽게 추가 할 수 있습니다.

1

위의 내용은 저에게 적합하지 않았습니다. 나는 이것을 관리하기 위해 노력했다. 처음에는 4 년 만에 Java를 사용하기 때문에 이것이 향상 될 수 있다고 확신합니다.

public static boolean robotSafe(URL url) 
{ 
    String strHost = url.getHost(); 

    String strRobot = "http://" + strHost + "/robots.txt"; 
    URL urlRobot; 
    try { urlRobot = new URL(strRobot); 
    } catch (MalformedURLException e) { 
     // something weird is happening, so don't trust it 
     return false; 
    } 

    String strCommands; 
    try 
    { 
     InputStream urlRobotStream = urlRobot.openStream(); 
     byte b[] = new byte[1000]; 
     int numRead = urlRobotStream.read(b); 
     strCommands = new String(b, 0, numRead); 
     while (numRead != -1) { 
      numRead = urlRobotStream.read(b); 
      if (numRead != -1) 
      { 
        String newCommands = new String(b, 0, numRead); 
        strCommands += newCommands; 
      } 
     } 
     urlRobotStream.close(); 
    } 
    catch (IOException e) 
    { 
     return true; // if there is no robots.txt file, it is OK to search 
    } 

    if (strCommands.contains(DISALLOW)) // if there are no "disallow" values, then they are not blocking anything. 
    { 
     String[] split = strCommands.split("\n"); 
     ArrayList<RobotRule> robotRules = new ArrayList<>(); 
     String mostRecentUserAgent = null; 
     for (int i = 0; i < split.length; i++) 
     { 
      String line = split[i].trim(); 
      if (line.toLowerCase().startsWith("user-agent")) 
      { 
       int start = line.indexOf(":") + 1; 
       int end = line.length(); 
       mostRecentUserAgent = line.substring(start, end).trim(); 
      } 
      else if (line.startsWith(DISALLOW)) { 
       if (mostRecentUserAgent != null) { 
        RobotRule r = new RobotRule(); 
        r.userAgent = mostRecentUserAgent; 
        int start = line.indexOf(":") + 1; 
        int end = line.length(); 
        r.rule = line.substring(start, end).trim(); 
        robotRules.add(r); 
       } 
      } 
     } 

     for (RobotRule robotRule : robotRules) 
     { 
      String path = url.getPath(); 
      if (robotRule.rule.length() == 0) return true; // allows everything if BLANK 
      if (robotRule.rule == "/") return false;  // allows nothing if/

      if (robotRule.rule.length() <= path.length()) 
      { 
       String pathCompare = path.substring(0, robotRule.rule.length()); 
       if (pathCompare.equals(robotRule.rule)) return false; 
      } 
     } 
    } 
    return true; 
} 

그리고 당신은 도우미 클래스가 필요합니다 : 질문이 정확히 무엇

/** 
* 
* @author Namhost.com 
*/ 
public class RobotRule 
{ 
    public String userAgent; 
    public String rule; 

    RobotRule() { 

    } 

    @Override public String toString() 
    { 
     StringBuilder result = new StringBuilder(); 
     String NEW_LINE = System.getProperty("line.separator"); 
     result.append(this.getClass().getName() + " Object {" + NEW_LINE); 
     result.append(" userAgent: " + this.userAgent + NEW_LINE); 
     result.append(" rule: " + this.rule + NEW_LINE); 
     result.append("}"); 
     return result.toString(); 
    }  
}