Java Jsoup选择表数据
就我而言,我不知道如何使用以“51u1FaI FHL.\u SL500\u AA300\u300.jpg”结尾的链接来选择img src 我尝试过多种方法,但没有一种有效。有什么帮助吗Java Jsoup选择表数据,java,jsoup,Java,Jsoup,就我而言,我不知道如何使用以“51u1FaI FHL.\u SL500\u AA300\u300.jpg”结尾的链接来选择img src 我尝试过多种方法,但没有一种有效。有什么帮助吗 doc1 = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad").timeout(20000).get(); Element table = doc1.select("table[class=p
doc1 = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad").timeout(20000).get();
Element table = doc1.select("table[class=productImageGrid]").first()
Iterator<Element> ite = table.select("td[height=300]").iterator();
doc1=Jsoup.connect(“http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad)超时(20000).get();
Element table=doc1.select(“table[class=productImageGrid]”)。first()
迭代器ite=table.select(“td[height=300]”。迭代器();
谢谢,
科迪
单击以查看更大的图像和其他视图
@user793728:尝试以下操作:-
document = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad").timeout(20000).get();
Elements elements =document.select(".prod_image_selector");
for (Element element : elements){
Attributes imageAttributes=element.attributes();
for (Attribute attribute: imageAttributes){
if(attribute.getKey().equals("src")){
String imageURL=attribute.getValue();
}
}
}
这里的问题似乎是Amazon根据请求UserAgent向jsoup返回的HTML与向浏览器返回的HTML不同 我将UserAgent设置为一个已知的浏览器,并使用
#prodImage
ID选择元素,结果是OK
例如
返回http://ecx.images-amazon.com/images/I/51u1FaI-FHL._SL500_AA300_.jpg
为了解决此类问题,我建议输出doc.html()
并查看检索到的解析html,因为它可能不同于浏览器的查看源html(因为服务器可以返回不同的html,并且在整理html并将其构建到DOM之前查看源代码显示)
希望这有帮助
document = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad").timeout(20000).get();
Elements elements =document.select(".prod_image_selector");
for (Element element : elements){
Attributes imageAttributes=element.attributes();
for (Attribute attribute: imageAttributes){
if(attribute.getKey().equals("src")){
String imageURL=attribute.getValue();
}
}
}
Document doc = Jsoup.connect("http://www.amazon.com/gp/product/B0051HDDO2?ie=UTF8&ref=mas_faad")
.timeout(20000)
.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30")
.get();
Element img = doc.select("#prodImage").first();
System.out.println(img.attr("src"));