【问题标题】:Parsing html elements using BeautifulSoup使用 BeautifulSoup 解析 html 元素
【发布时间】:2013-05-22 22:53:59
【问题描述】:

假设我有:

<tr>
   <td class="prodSpecAtribute">word</td>
   <td colspan="5">
      <a href="http://www.cmegroup.com/clearing/trading-practices/CMEblock-trade.html" target="_blank">another_word</a>
   </td>
</tr>

我想提取 2 个 td 类中的文本(word 和 another_word: 所以我用了 BeautifulSoup:

这是 Matijn Pieters 要求的代码: 基本上,它从 html 页面(从表中)获取信息并将这些值存储在左右列列表中。然后,我根据这些详细信息创建一个字典(使用左列列表作为键,对于值,我使用右列列表)

def get_data(page):

soup = BeautifulSoup(page)

left = []
right = []

#Obtain data from table and store into left and right columns
#Iterate through each row
for tr in soup.findAll('tr'):

    #Find all table data(cols) in that row
    tds = tr.findAll('td')

    #Make sure there are 2 elements, a col and a row
    if len(tds) >= 2:

        #Find each entry in a row -> convert to text
        right_col = []
        inp = []
        once = 0
        no_class = 0
        for td in tds:
            if once == 0:               
                #Check if of class 'prodSpecAtribute'
                if check(td) == True:
                    left_col = td.findAll(text=True)
                    left_col_x = re.sub('&\w+;', '', str(left_col[0]))
                    once = 1
                else:
                    no_class = 1
                    break

            else:        
                right_col = td.findAll(text=True)
                right_col_x = ' '.join(text for text in right_col if text.strip())
                right_col_x = re.sub('&\w+;', '', right_col_x)
                inp.append(right_col_x)


        if no_class == 0:
            inps = '. '.join(inp)
            left.append(left_col_x)
            right.append(inps)

#Create a Dictionary for left and right cols
item = dict(zip(left, right))
return item

【问题讨论】:

  • 正在复制链接中的哪些信息?
  • 您的示例生成[u'word'][u'\n', u'another_word', u'\n'],后者包括a 标记围绕 的空格。这就是你看到的吗?
  • 只是&lt;td&gt;标签之间的文本
  • 请注意,您的循环每次替换 col;原始列表中没有附加任何内容。为此使用col.extend(td.findAll(text=True))
  • 您正在向它询问 &lt;td&gt; 标签内的所有文本元素;是否要过滤掉仅包含空格的文本?

标签: python html-parsing beautifulsoup


【解决方案1】:

您可以使用 HTQL (http://htql.net)。

这是你的例子:

import htql
page="""
   <tr>
      <td class="prodSpecAtribute">word</td>
      <td colspan="5">
          <a href="http://www.cmegroup.com/clearing/trading-practices/CMEblock-trade.html" target="_blank">another_word</a>
      </td>
   </tr>
   """

query = """
   <tr>{ 
      c1 = <td (class='prodSpecAtribute')>1 &tx;
      c2 = <td>2 &tx &trim;
   }
   """ 

a=htql.query(page, query)
print(dict(a))

打印出来:

{'word': 'another_word'}

【讨论】:

    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2012-12-13
    • 2016-04-18
    相关资源
    最近更新 更多