爬虫遇到路径转换的解决方案
生活随笔
收集整理的這篇文章主要介紹了
爬虫遇到路径转换的解决方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
String href = n.attr("abs:href");//jsoup自帶的路徑轉換方法,有的時候行不通if("".equals(href)) {href = n.attr("href");if (href.indexOf("http") < 0) {href = getAbsoluteURL(url, href);}}
@SuppressWarnings("finally")public static String getAbsoluteURL(String baseURI, String relativePath) {String abURL = null;try {URI base = new URI(baseURI);// 基本網頁URIURI abs = base.resolve(relativePath);// 解析于上述網頁的相對URL,得到絕對URIURL absURL = abs.toURL();// 轉成URLabURL = absURL.toString();} catch (MalformedURLException e) {e.printStackTrace();} catch (URISyntaxException e) {e.printStackTrace();} finally {return abURL;}}
總結
以上是生活随笔為你收集整理的爬虫遇到路径转换的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3元类简介(metaclas
- 下一篇: 使用注解配置spring如@Compne