目的:获取网站链接,实现无人工干预的资料获取。
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 codecs
codecs.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上最好用的中文输入法!, 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