jsoup从amazon.comlink爬取图片宽高
jsoup crawling image width and height from amazon.com link
以下是亚马逊示例 link 我正在尝试抓取图像的宽度和高度:
http://images.amazon.com/images/P/0099441365.01.SCLZZZZZZZ.jpg
我正在使用 jsoup,下面是我的代码:
import java.io.*;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Crawler_main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String filepath = "C:/imagelinks.txt";
try (BufferedReader br = new BufferedReader(new FileReader(filepath))) {
String line;
String width;
//String height;
while ((line = br.readLine()) != null) {
// process the line.
System.out.println(line);
Document doc = Jsoup.connect(line).ignoreContentType(true).get();
//System.out.println(doc.toString());
Elements jpg = doc.getElementsByTag("img");
width = jpg.attr("width");
System.out.println(width);
//String title = doc.title();
}
}
catch (FileNotFoundException ex){
System.out.println("File not found");
}
catch(IOException ex){
System.out.println("Unable to read line");
}
catch (Exception ex){
System.out.println("Exception occured");
}
}
}
html 已获取,但当我提取宽度属性时,它 returns 为空。当我打印获取的 html 时,它包含垃圾字符(我猜它是我称之为垃圾字符的实际图像信息。例如:
我什至无法将 document.toString() 结果粘贴到此编辑器中。求助!
问题是您获取的是 jpg 文件,而不是任何 HTML。对 ignoreContentType(true) 的调用提供了一个线索,因为它的 documentation 声明:
Ignore the document's Content-Type when parsing the response. By default this is false, an unrecognised content-type will cause an IOException to be thrown. (This is to prevent producing garbage by attempting to parse a JPEG binary image, for example.)
如果要获取实际jpg文件的宽度,this SO answer可能有用:
BufferedImage bimg = ImageIO.read(new File(filename));
int width = bimg.getWidth();
int height = bimg.getHeight();
以下是亚马逊示例 link 我正在尝试抓取图像的宽度和高度:
http://images.amazon.com/images/P/0099441365.01.SCLZZZZZZZ.jpg
我正在使用 jsoup,下面是我的代码:
import java.io.*;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class Crawler_main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String filepath = "C:/imagelinks.txt";
try (BufferedReader br = new BufferedReader(new FileReader(filepath))) {
String line;
String width;
//String height;
while ((line = br.readLine()) != null) {
// process the line.
System.out.println(line);
Document doc = Jsoup.connect(line).ignoreContentType(true).get();
//System.out.println(doc.toString());
Elements jpg = doc.getElementsByTag("img");
width = jpg.attr("width");
System.out.println(width);
//String title = doc.title();
}
}
catch (FileNotFoundException ex){
System.out.println("File not found");
}
catch(IOException ex){
System.out.println("Unable to read line");
}
catch (Exception ex){
System.out.println("Exception occured");
}
}
}
html 已获取,但当我提取宽度属性时,它 returns 为空。当我打印获取的 html 时,它包含垃圾字符(我猜它是我称之为垃圾字符的实际图像信息。例如:
我什至无法将 document.toString() 结果粘贴到此编辑器中。求助!
问题是您获取的是 jpg 文件,而不是任何 HTML。对 ignoreContentType(true) 的调用提供了一个线索,因为它的 documentation 声明:
Ignore the document's Content-Type when parsing the response. By default this is false, an unrecognised content-type will cause an IOException to be thrown. (This is to prevent producing garbage by attempting to parse a JPEG binary image, for example.)
如果要获取实际jpg文件的宽度,this SO answer可能有用:
BufferedImage bimg = ImageIO.read(new File(filename));
int width = bimg.getWidth();
int height = bimg.getHeight();