获取维基百科文章中不在括号内的第一个链接

时间:2012-05-17 10:48:57

标签: python parsing dom

所以我对this theory感兴趣,如果你去一篇随机的维基百科文章,请点击不在括号内的第一个链接,在95%的情况下你最终将会发表关于{{3的文章}}。

我想在Python中编写一个脚本,为我提取链接,最后打印一个很好的列表,列出了哪些文章被访问过(linkA -> linkB -> linkC)等。

我设法获取了网页的HTML DOM,并设法删除了一些不必要的链接以及引导消歧页面的顶级描述栏。到目前为止,我已经得出结论:

  • DOM从您在某些页面右侧看到的表开始,例如在Philosophy中。我们想忽略这些链接。
  • 有效的链接元素在某处都有一个<p>元素作为其祖先(如果它位于<b>标记或类似内容中,通常是父母或祖父母。导致消歧页面的顶部栏不会似乎包含任何<p>元素。
  • 无效的链接包含一些特殊单词后跟冒号,例如Wikipedia:

到目前为止,这么好。但这是括号,让我。例如,在关于Human的文章中,不在括号内的第一个链接是“/ wiki / Species”,但脚本在其中找到“/ wiki / Taxonomy”。

我不知道如何以编程方式进行此操作,因为我必须在父/子节点的某些组合中查找文本,这些组合可能并不总是相同。有什么想法吗?

我的代码可以在下面看到,但这是我很快就编写的东西而不是非常自豪。然而,它被评论,所以你可以看到我的想法(我希望:))。

"""Wikipedia fun"""
import urllib2
from xml.dom.minidom import parseString
import time

def validWikiArticleLinkString(href):
    """ Takes a string and returns True if it contains the substring
        '/wiki/' in the beginning and does not contain any of the
        "special" wiki pages. 
    """
    return (href.find("/wiki/") == 0
            and href.find("(disambiguation)") == -1 
            and href.find("File:") == -1 
            and href.find("Wikipedia:") == -1
            and href.find("Portal:") == -1
            and href.find("Special:") == -1
            and href.find("Help:") == -1
            and href.find("Template_talk:") == -1
            and href.find("Template:") == -1
            and href.find("Talk:") == -1
            and href.find("Category:") == -1
            and href.find("Bibcode") == -1
            and href.find("Main_Page") == -1)


if __name__ == "__main__":
    visited = []    # a list of visited links. used to avoid getting into loops

    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')] # need headers for the api

    currentPage = "Human"  # the page to start with

    while True:
        infile = opener.open('http://en.wikipedia.org/w/index.php?title=%s&printable=yes' % currentPage)
        html = infile.read()    # retrieve the contents of the wiki page we are at

        htmlDOM = parseString(html) # get the DOM of the parsed HTML
        aTags = htmlDOM.getElementsByTagName("a")   # find all <a> tags

        for tag in aTags:
            if "href" in tag.attributes.keys():         # see if we have the href attribute in the tag
                href = tag.attributes["href"].value     # get the value of the href attribute
                if validWikiArticleLinkString(href):                             # if we have one of the link types we are looking for

                    # Now come the tricky parts. We want to look for links in the main content area only,
                    # and we want the first link not in parentheses.

                    # assume the link is valid.
                    invalid = False            

                    # tables which appear to the right on the site appear first in the DOM, so we need to make sure
                    # we are not looking at a <a> tag somewhere inside a <table>.
                    pn = tag.parentNode                     
                    while pn is not None:
                        if str(pn).find("table at") >= 0:
                            invalid = True
                            break
                        else:
                            pn = pn.parentNode 

                    if invalid:     # go to next link
                        continue               

                    # Next we look at the descriptive texts above the article, if any; e.g
                    # This article is about .... or For other uses, see ... (disambiguation).
                    # These kinds of links will lead into loops so we classify them as invalid.

                    # We notice that this text does not appear to be inside a <p> block, so
                    # we dismiss <a> tags which aren't inside any <p>.
                    pnode = tag.parentNode
                    while pnode is not None:
                        if str(pnode).find("p at") >= 0:
                            break
                        pnode = pnode.parentNode
                    # If we have reached the root node, which has parentNode None, we classify the
                    # link as invalid.
                    if pnode is None:
                        invalid = True

                    if invalid:
                        continue


                    ######  this is where I got stuck:
                    # now we need to look if the link is inside parentheses. below is some junk

#                    for elem in tag.parentNode.childNodes:
#                        while elem.firstChild is not None:
#                            elem = elem.firstChid
#                        print elem.nodeValue

                    print href      # this will be the next link
                    newLink = href[6:]  # except for the /wiki/ part
                    break

        # if we have been to this link before, break the loop
        if newLink in visited:
            print "Stuck in loop."
            break
        # or if we have reached Philosophy
        elif newLink == "Philosophy":
            print "Ended up in Philosophy."
            break
        else:
            visited.append(currentPage)     # mark this currentPage as visited
            currentPage = newLink           # make the the currentPage we found the new page to fetch
            time.sleep(5)                   # sleep some to see results as debug

1 个答案:

答案 0 :(得分:3)

我在Github上发现了一个python脚本(http://github.com/JensTimmerman/scripts/blob/master/philosophy.py)来玩这个游戏。 它使用Beautifulsoup进行HTML解析并处理parantheses问题,他只是在解析链接之前删除括号中的文本。