本帖最後由 hl2dm 於 2012-12-10 12:01 編輯
剛剛以奇摩搜尋測試了一下- private void now() {
- String webserviceurl = "http://tw.search.yahoo.com/search/images;_ylt=A8tUwJjRU8VQD3gAiBFs1gt.";
- InputStream is;
- List<NameValuePair> lstAddToken = new ArrayList<NameValuePair>();
- lstAddToken.add(new BasicNameValuePair("fr","sfp"));
- lstAddToken.add(new BasicNameValuePair("p","java"));
- lstAddToken.add(new BasicNameValuePair("iscqry",""));
- lstAddToken.add(new BasicNameValuePair("ei","utf-8"));
- //add parameters to the URL
- webserviceurl += "?";
- String paramString = URLEncodedUtils.format(lstAddToken, "utf-8");
- webserviceurl += paramString;
- //Call the webservice using HttpGet with parameters and get the response from webservice
- try
- {
- DefaultHttpClient demo = new DefaultHttpClient();
- demo.getParams().setParameter("http.protocol.content-charset", "UTF-8");
- // Get Request Example,取得 google 查詢 httpclient 的結果
- HttpGet httpGet = new HttpGet(webserviceurl);
- HttpResponse response2 = demo.execute(httpGet);
- String responseString = EntityUtils.toString(response2.getEntity());
- Document doc = Jsoup.parse(responseString);
- Elements pngs = doc.select("img[src]");
- if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
- // 如果回傳是 200 OK 的話才輸出
- System.out.println(responseString);
- System.out.println(pngs);
- } else {
- System.out.println(response2.getStatusLine());
- }
- }
- catch (Throwable t)
- {
- Log.e("log_tag", "Error converting result "+t.toString());
- }
-
- }
複製代碼 得到回傳- <img width="1" height="1" alt="" src="http://row.bc.yahoo.com/b?P=mz_sXctUwKeTXvBXEmE7Qw_tdw5QhFDFWjYADuBj&T=1847ko0p4%2fX%3d1355110967%2fE%3d2114705005%2fR%3dtwsrcgspi%2fK%3d5%2fV%3d2.1%2fW%3dHR%2fY%3dKIMO%2fF%3d3387896648%2fH%3dc2VydmVJZD0ibXpfc1hjdFV3S2VUWHZCWEVtRTdRd190ZHc1UWhGREZXallBRHVCaiIgc2l0ZUlkPSIyNDI1NjEiIHRTdG1wPSIxMzU1MTEwOTY3ODU1NjA5IiA-%2fQ%3d-1%2fS%3d1%2fJ%3d99C054CB&U=12bep1lpl%2fN%3dWD4uMsorwsw-%2fC%3d-1%2fD%3dNT1%2fB%3d-1%2fV%3d0" />
- <img src="http://ts3.mm.bing.net/th?id=H.4747137966868250&pid=15.1" width="145" alt="" height="145" />
- <img src="http://ts4.mm.bing.net/th?id=H.4573702904743551&pid=15.1" width="194" alt="" height="145" style="margin-left:-25px;" />
- <img src="http://ts1.mm.bing.net/th?id=H.4937430769337608&pid=15.1" width="182" alt="" height="145" style="margin-left:-19px;" />
- <img src="http://ts3.mm.bing.net/th?id=I.4872666953155418&pid=15.1" width="218" alt="" height="145" style="margin-left:-35px;" />
- <img src="http://ts4.mm.bing.net/th?id=I.4819765862138207&pid=15.1" width="191" alt="" height="145" style="margin-left:-23px;" />
- <img src="http://ts2.mm.bing.net/th?id=H.4619341236602193&pid=15.1" width="182" alt="" height="145" style="margin-left:-36px;" />
- <img src="http://ts1.mm.bing.net/th?id=H.4854653888105216&pid=15.1" width="195" alt="" height="145" style="margin-left:-25px;" />
- <img src="http://ts4.mm.bing.net/th?id=H.4709827606611127&pid=15.1" width="294" alt="" height="145" style="margin-left:-149px;" />
- <img src="http://ts1.mm.bing.net/th?id=H.4773680868623148&pid=15.1" width="145" alt="" height="145" />
- <img src="http://ts3.mm.bing.net/th?id=I.4665129879470998&pid=15.1" width="145" alt="" height="145" />
複製代碼 不過還是除了圖片連結外又多了一堆orz alt height width 要怎麼弄掉還是不會LOL
於是我又測試了- Elements pngs = doc.select("img[src]").removeAttr("style").removeAttr("width").removeAttr("alt").removeAttr("height").removeAttr("pid");
複製代碼 成功達到我的目的了...
奇怪我之前在家裡面測試就不行@"@
原來removeAttr這個功能有用!!
回家再測試
因為該網站不適合在公司開XXD |