網(wǎng)頁(yè)爬蟲(chóng) - Java爬蟲(chóng)已獲取圖片鏈接但是無(wú)法下載圖片
問(wèn)題描述
利用爬蟲(chóng)在html中獲取的相應(yīng)圖片資源src的代碼是這樣的
但是再通過(guò)代碼將資源轉(zhuǎn)成鏈接的形式下載圖片的時(shí)候,就報(bào)了400的錯(cuò)誤
然而,我使用chrome去測(cè)試鏈接是否存在是,發(fā)現(xiàn),真正對(duì)方網(wǎng)站服務(wù)器能夠識(shí)別的是
也就是說(shuō)我通過(guò)網(wǎng)頁(yè)獲得圖片資源的鏈接是http://www.neofactory.co.jp/i... 2.jpg然而,正常能夠獲取圖片的鏈接是http://www.neofactory.co.jp/i...
請(qǐng)各位大神指導(dǎo)之后應(yīng)該怎么辦,我在網(wǎng)上查了好多資料,還是沒(méi)有解決辦法。ps:奇怪的是我用Firefox的話,上面的那個(gè)鏈接也能得到圖片,我就百思不得其解了。
代碼:
public class Image {
private String urlNeo='';public String getUrlNeo() { return urlNeo;}public void setUrlNeo(String urlNeo) { this.urlNeo = urlNeo;}public String getHtml() throws Exception{ ArrayList<String> list=new ArrayList<String>();String line=''; String Html=''; URL url=new URL(urlNeo); URLConnection connection=url.openConnection(); InputStream in=connection.getInputStream(); InputStreamReader isr=new InputStreamReader(in); BufferedReader br=new BufferedReader(isr); while((line=br.readLine())!=null){Html+=line;list.add(line); } br.close(); isr.close(); in.close(); return Html;}public String getImgSrc() throws Exception{ String html=getHtml(); String IMGURL_REG_xpath='//p[1]/p[2]/p[2]/p/node()'; String imginfomation=''; JXDocument jxDocument = new JXDocument(html); imginfomation=(jxDocument.sel(IMGURL_REG_xpath).toString()).substring(1,jxDocument.sel(IMGURL_REG_xpath).toString().length() - 1); return imginfomation;}public List<String> getImgXpath() throws Exception{ String str=''; String IMGSRC_REG = 'img.product.w.*.jpg'; List<String> list1=new ArrayList<String>(); List<String> list2=new ArrayList<String>(); String listimg = getImgSrc(); Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(listimg); while (matcher.find()) {list1.add(matcher.group()); } for(int i=1;i<=(list1.size()/2);i++){int j=i*2;list2.add(list1.get(j-1)); } return list2;}public void download(String admin_no) throws Exception{ List<String> list=new ArrayList<String>(); list=getImgXpath(); for(String img:list){System.out.println(img);String url='http://www.neofactory.co.jp/'+img;URL uri=new URL(url);URLConnection con=uri.openConnection();con.setConnectTimeout(5000);InputStream in=con.getInputStream();byte[] buf=new byte[1024];int length=0; File sf=new File('D:item_neo_photo'+admin_no);if(!sf.exists()){ sf.mkdirs();}String[] a=img.split('/');OutputStream os=new FileOutputStream(sf.getPath()+''+a[a.length-1]);while((length=in.read(buf))!=-1){ os.write(buf, 0, length);}os.close();in.close(); }}
}
問(wèn)題解答
回答1:直接把域名+獲取的img src屬性拼起來(lái)不行么
回答2:url編碼下
相關(guān)文章:
1. javascript - 有適合開(kāi)發(fā)手機(jī)端Html5網(wǎng)頁(yè)小游戲的前端框架嗎?2. python - pandas按照列A和列B分組,將列C求平均數(shù),怎樣才能生成一個(gè)列A,B,C的dataframe3. html - eclipse 標(biāo)簽錯(cuò)誤4. javascript - axios請(qǐng)求回來(lái)的數(shù)據(jù)組件無(wú)法進(jìn)行綁定渲染5. python - Pycharm的Debug用不了6. 安全性測(cè)試 - nodejs中如何防m(xù)ySQL注入7. javascript - 關(guān)于apply()與call()的問(wèn)題8. javascript - JS變量被清空9. python文檔怎么查看?10. python - pycharm 自動(dòng)刪除行尾空格
