【问题标题】:Tcl Tk show all available linksTcl Tk 显示所有可用链接
【发布时间】:2017-02-12 17:30:09
【问题描述】:

Tcl/Tk 中是否有显示链接中所有可用 url 的功能?我想开始编写一个具有一些功能的网络爬虫。

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

【问题讨论】:

    标签: url hyperlink tree tcl tk


    【解决方案1】:

    使用 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]
    }
    

    【讨论】:

    • 当我在公司内部网首页上运行它时,它似乎可以工作,但我认为我不会分发链接列表......
    • 如果文档有a元素而没有href属性(例如名称锚点),可以使用foreach href [$doc selectNodes {//a/@href}] {puts [lindex $href end]}
    • @PeterLewerin 我认为在这种情况下,XPath 最好是//a[@href];你不想要属性节点,你只想指定它们在那里。
    • 这也有效。当我只想要属性的值而不关心节点时,我会像我之前的评论那样做。
    • 只是属性节点在DOM中很奇怪,我尽量避免直接看它们。 ;-)
    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多