【问题标题】:Extract information in a span over multiple HTML documents在多个 HTML 文档中提取信息
【发布时间】:2013-03-09 13:51:44
【问题描述】:

我有一个问题,我有大约 700 个 html 文档,每个文档都包含一个包含在跨度中的字母,所有这些都在给定的同一类中。

有没有办法取出所有字母并将它们连接在一起?也许使用 BeautifulSoup 或其他方法?

【问题讨论】:

    标签: html beautifulsoup extraction


    【解决方案1】:

    当然有。试试这样的:

    import os
    from BeautifulSoup import BeautifulSoup
    
    letter_list = []
    for file in os.listdir('path/to/dir'):
        with open('path/to/file', 'r') as html_file:
            html = ' '.join(str(x) for x in list(html_file)) # Combines each row in file into a single string
            soup = BeautifulSoup(html)
    
            letter = soup('span',{'class':'someclass'})[0].contents[0]
            letter_list.append(letter)
    
    my_string = ''.join(str(x) for x in letter_list)
    

    这将遍历目录,打开每个 html 文件并解析字符串。提取的字母会附加到一个列表中,并在解析完所有文件后加入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 2012-07-04
      相关资源
      最近更新 更多