網頁爬蟲 - Java爬蟲已獲取圖片鏈接但是無法下載圖片
問題描述
利用爬蟲在html中獲取的相應圖片資源src的代碼是這樣的
但是再通過代碼將資源轉成鏈接的形式下載圖片的時候,就報了400的錯誤
然而,我使用chrome去測試鏈接是否存在是,發現,真正對方網站服務器能夠識別的是
也就是說我通過網頁獲得圖片資源的鏈接是http://www.neofactory.co.jp/i... 2.jpg然而,正常能夠獲取圖片的鏈接是http://www.neofactory.co.jp/i...
請各位大神指導之后應該怎么辦,我在網上查了好多資料,還是沒有解決辦法。ps:奇怪的是我用Firefox的話,上面的那個鏈接也能得到圖片,我就百思不得其解了。
代碼:
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(); }}
}
問題解答
回答1:直接把域名+獲取的img src屬性拼起來不行么
回答2:url編碼下
相關文章:
1. 在應用配置文件 app.php 中找不到’route_check_cache’配置項2. html按鍵開關如何提交我想需要的值到數據庫3. mysql取模分表與分表4. gvim - 誰有vim里CSS的Indent文件, 能縮進@media里面的5. HTML 5輸入框只能輸入漢字、字母、數字、標點符號?正則如何寫?6. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下7. 跟著課件一模一樣的操作使用tp6,出現了錯誤8. PHP類屬性聲明?9. objective-c - ios 怎么實現微信聯系列表 最好是swift10. javascript - 請教如何獲取百度貼吧新增的兩個加密參數
