2016-10-04 71 views
0

Tcl/Tk中是否有一個函數來顯示鏈接中所有可用的URL?我想開始編寫具有一些功能的web爬行器。Tcl Tk顯示所有可用鏈接

For example: 
the user types this: 
"www.testsite.com" 
and he will get that: 
"www.testsite.com/dir1/" 
"www.testsite.com/dir2/" 
e.g. 

或者用像phyton這樣的其他語言編程它會更好嗎?

br

回答

2

使用http和tDOM包很容易。你只需要知道一點的XPath ......

package require http 
package require tdom 

set tok [http::geturl http://example.com/index.html] 
set html [http::data $tok] 
http::cleanup $tok 

set doc [dom parse -html $html] 
foreach anchor [$doc selectNodes "//a"] { 
    puts [$anchor @href] 
} 
+0

看來當我運行它針對我公司的Intranet頭版工作,雖然我不認爲如果文檔有我會發布的鏈接列表... –

+0

沒有'href'屬性的'a'元素(例如名字錨),可以使用'foreach href [$ doc selectNodes {// a/@ href}] {puts [lindex $ href end]}''。 –

+0

@PeterLewerin我_think_ XPath在這種情況下最好是'// a [@href]';你不需要屬性節點,你只是想指定它們在那裏。 –