여기 내 문제가 있습니다. "sites.txt"라는 txt 파일이 있습니다. 이 i 유형 무작위 인터넷 사이트에서. 내 목표는 각 사이트의 첫 번째 이미지를 저장하는 것입니다. img 태그로 서버 응답을 필터링하려고했지만 일부 사이트에서는 실제로 작동하지만 일부에서는 작동하지 않습니다.URL의 첫 번째 이미지 저장
img src에서 작동하는 사이트는 http : // ...로 시작합니다. 사이트는 작동하지 않는 사이트로 시작합니다.
나는 또한 HTTP를 추가하려고 : // 그것을 가지고 didnt가 지정한 IMG SRC의 이미지,하지만 난 여전히 같은 오류 얻을 :Exception in thread "main" java.net.MalformedURLException: no protocol:
at java.net.URL.<init>(Unknown Source)
내 현재 코드는 다음과 같습니다
public static void main(String[] args) throws IOException{
try {
File file = new File ("sites.txt");
Scanner scanner = new Scanner (file);
String url;
int counter = 0;
while(scanner.hasNext())
{
url=scanner.nextLine();
URL page = new URL(url);
URLConnection yc = page.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine = in.readLine();
while (!inputLine.toLowerCase().contains("img"))inputLine = in.readLine();
in.close();
String[] parts = inputLine.split(" ");
int i=0;
while(!parts[i].contains("src"))i++;
String destinationFile = "image"+(counter++)+".jpg";
saveImage(parts[i].substring(5,parts[i].length()-1), destinationFile);
String tmp=scanner.nextLine();
System.out.println(url);
}
scanner.close();
}
catch (FileNotFoundException e)
{
System.out.println ("File not found!");
System.exit (0);
}
}
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
// TODO Auto-generated method stub
URL url = new URL(imageUrl);
String fileName = url.getFile();
String destName = fileName.substring(fileName.lastIndexOf("/"));
System.out.println(destName);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
을
나는 또한 apache jakarte http 클라이언트 라이브러리를 사용하는 팁을 가지고있다. 그러나 나는 그들이 내가 어떤 도움을 주겠다고 어떻게 사용할 수 있는지 전혀 모른다.
http://hc.apache.org/httpcomponents-client-ga/examples.html – px5x2
에서 몇 가지 예제를 살펴볼 수 있습니다. [jsoup] (http : // jsoup. org /)를 사용하면 HTML을 매우 쉽게 파싱 할 수 있습니다. 구성표가 누락 된 이미지 URL을 실행할뿐만 아니라 ** 상대 ** 경로도 실행하게되므로 사이트의 URL에 추가해야합니다. 예를 들어' ''이 표시되며,'https://www.google.com/ ''에 추가해야합니다. – sgbj
텍스트 파일에 URL의 몇 가지 샘플을 표시 할 수 있습니까? 영업 게시물 – Braj