Python解析用户定义的值

时间:2011-07-01 19:27:10

标签: python html parsing url

我试图改变: -

import urllib2 as urllib
... ...
file2 = urllib.urlopen(url2)
... ...
for line in file2:
    indexfrom2 = line.find('Mean Temperature')
    if indexfrom2 > -1:
        nxtLn = file2.next()
        nextLine = file2.next()
        indexfrom21 = nextLine.find('"nobr"')
        if indexfrom21 > -1:
            indexto21 = nextLine.find('</span>&nbsp;&deg;C</span>',indexfrom21)
        code2 = nextLine[indexfrom21+23:indexto21]
        print code2

并使其看起来像: -

class (...)  
def ....  
Temperature = parse( file2, '<span>Mean Temperature</span></td>', '<b>' )  

但我不知道该怎么做。我要解析的上述代码集是针对不同的值重复的,我想使用解析函数来保持简短,以便它形成一个集合或循环,我不必一次又一次地重复所有代码。 [对于每个值(如平均温度,最高温度,湿度,压力等),代码在我的脚本上重复,有点看起来不专业]。

1 个答案:

答案 0 :(得分:1)

你可能想要使用BeautifulSoup。这是解析HTML的规范方法(即使在一些可怕的边缘情况下它也能很好地工作)。如果你继续使用当前的方法,那么你就依赖于行号这样的东西,因此面对轻微的文档结构变化,你的代码就会非常脆弱。

http://www.crummy.com/software/BeautifulSoup/