Jython使用jsoup擷取網頁標題與連結資訊

來源:互聯網
上載者:User

標籤:blog   http   io   os   使用   java   ar   for   2014   

目的:擷取網站連結,可以實現無人工幹預的資料擷取。


1 java實現的jsoup HTML解析庫

下載:http://jsoup.org/


2 工作平台Ubuntu

3 使用Jython調用jsoup實現提取網頁聯結資訊

代碼:

#coding=utf-8#doc from http://jsoup.org/apidocs/from org.python.core import codecscodecs.setDefaultEncoding('utf-8')import sys#print(sys.defaultencoding)sys.path.append("/home/xxx/software/htmlparse/jsoup-1.7.3.jar");from org.jsoup import *doc = Jsoup.connect("http://www.baidu.com").get();elms = doc.getAllElements();head = elms.select("head")page_title = head.text()print(page_title)hrfs = elms.select("[href^=http]")for h in hrfs:title = h.text()url = h.attr('href')print title + ", " + url

效果如下:

百度一下,你就知道
體驗iPhone上最好用的中文IME!, http://srf.baidu.com/ios8/pc.html
登入, https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
新聞, http://news.baidu.com
hao123, http://www.hao123.com
地圖, http://map.baidu.com
視頻, http://v.baidu.com
貼吧, http://tieba.baidu.com
登入, https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F
設定, http://www.baidu.com/gaoji/preferences.html
更多產品, http://www.baidu.com/more/
新聞, http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=
貼吧, http://tieba.baidu.com/f?kw=&fr=wwwt
知道, http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt
音樂, http://music.baidu.com/search?fr=ps&key=
圖片, http://image.baidu.com/i?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&word=
視頻, http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=
地圖, http://map.baidu.com/m?word=&fr=ps01000
文庫, http://wenku.baidu.com/search?word=&lm=0&od=0
把百度設為首頁, http://www.baidu.com/cache/sethelp/index.html
關於百度, http://home.baidu.com
About Baidu, http://ir.baidu.com

Jython使用jsoup擷取網頁標題與連結資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.