我想從HTML源代碼中提取一些數據到我的Java項目。 html取自「必應搜索圖片」,我想從<a>
標籤獲取所有圖片。這是html代碼:如何從HTML中提取多個值到java?
<a href="/images/search?q=nba&view=detailv2&&&
id=FE19E7BB2916CE8B6CD78148F3BC0656D151049A&
selectedIndex=3&
ccid=2%2f7OBkGc&
simid=608035681734625885&
thid=JN.tdPCsRj4HyJzbwA%2bgXsS8g"
ihk="JN.tdPCsRj4HyJzbwA+gXsS8g"
m="{ns:"images",k:"5070",dirovr:"ltr",
mid:"FE19E7BB2916CE8B6CD78148F3BC0656D151049A",
surl:"http://www.nba.com/gallery/rookie/070727_1.html",
imgurl:"http://www.nba.com/media/draft_class_3_07_070727.jpg
",
ow:"300",docid:"608035681734625885",oh:"192",tft:"58"}"
mid="FE19E7BB2916CE8B6CD78148F3BC0656D151049A"
t1="The 2007 NBA Draft Class"
t2="625 x 400 · 374 kB · jpeg"
t3="www.nba.com/gallery/rookie/070727_1.html"
h="ID=images,5070.1"><img data-bm="16"
src="https://tse3.mm.bing.net/th?id=JN.tdPCsRj4HyJzbwA%2bgXsS8g&w=217&h=142&c=7&rs=1&qlt=90&o=4&pid=1.1"
style="width:217px;height:142px;" width="217" height="142">
</a>
,這是我如何試圖提取它,但沒有成功:
public static void main(String[] args) {
String title = "dog";
String url = "https://www.bing.com/images/search?q="+title+"&FORM=HDRSC2";
try {
Document doc = Jsoup.connect(url).get();
Elements img = doc.getElementsByTag("a");
for (Element el : img) {
String src1 = el.absUrl("imgurl");
String src2 = el.absUrl("surl");
System.out.println(src1 + " " + src2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
,如果有可能的任何想法?
在哪一點你沒有成功? 'Jsonp.connect'或者'for loop'裏面?要麼 ? – pmverma
連接工作正常,但沒有結果'String src1 = el.absUrl(「imgurl」); String src2 = el.absUrl(「surl」);' –
您是否在使用任何IDE進行開發?如果是這樣,請嘗試調試並找到正確的表達式。 – pmverma