【问题标题】:extract class name from tag beautifulsoup python从标签beautifulsoup python中提取类名
【发布时间】:2014-02-06 01:05:20
【问题描述】:

我有以下 HTML 代码:

    <td class="image">
      <a href="/target/tt0111161/" title="Target Text 1">
       <img alt="target img" height="74" src="img src url" title="image title" width="54"/>
      </a>
     </td>
     <td class="title">
      <span class="wlb_wrapper" data-caller-name="search" data-size="small" data-tconst="tt0111161">
      </span>
      <a href="/target/tt0111161/">
       Other Text
      </a>
      <span class="year_type">
       (2013)
      </span>

我正在尝试使用漂亮的汤将某些元素解析为制表符分隔的文件。 我得到了一些很大的帮助并且有:

for td in soup.select('td.title'):
 span = td.select('span.wlb_wrapper')
 if span:
     print span[0].get('data-tconst') # To get `tt0082971`

现在我想获取“目标文本 1”。

我已经尝试了一些类似上述文本的内容,例如:

for td in soup.select('td.image'): #trying to select the <td class="image"> tag
img = td.select('a.title') #from inside td I now try to look inside the a tag that also has the word title
if img:
    print img[2].get('title') #if it finds anything, then I want to return the text in class 'title'

【问题讨论】:

标签: python html parsing beautifulsoup


【解决方案1】:

如果您尝试根据类获取不同的 td(即 td class="image" 和 td class="title",您可以使用 beautiful soup 作为字典来获取不同的类。

这将在表中找到所有 td class="image"。

from bs4 import BeautifulSoup

page = """
<table>
    <tr>
        <td class="image">
           <a href="/target/tt0111161/" title="Target Text 1">
            <img alt="target img" height="74" src="img src url" title="image title" width="54"/>
           </a>
          </td>
          <td class="title">
           <span class="wlb_wrapper" data-caller-name="search" data-size="small" data-tconst="tt0111161">
           </span>
           <a href="/target/tt0111161/">
            Other Text
           </a>
           <span class="year_type">
            (2013)
           </span>
        </td>
    </tr>
</table>
"""
soup = BeautifulSoup(page)
tbl = soup.find('table')
rows = tbl.findAll('tr')
for row in rows:
    cols = row.find_all('td')
    for col in cols:
        if col.has_attr('class') and col['class'][0] == 'image':
            hrefs = col.find_all('a')
            for href in hrefs:
                print href.get('title')

        elif col.has_attr('class') and col['class'][0] == 'title':
            spans = col.find_all('span')
            for span in spans:
                if span.has_attr('class') and span['class'][0] == 'wlb_wrapper':
                    print span.get('data-tconst')

【讨论】:

  • 谢谢,我还可以添加语句来检索“data-tconst”标签的值吗?
  • 是的,您可以添加一个 elif 语句来查找带有标题的 td,在注释中粘贴代码失败,所以我将更新我的答案。
  • 谢谢,现在我刚刚添加了def getinfo:。我可以将 getinfo 写入 CSV 吗?
  • 我个人从未写入 csv,但您应该能够在迭代之前打开文件,而不是打印出值,而是将它们写入文件。在迭代器之后,保存文件。
【解决方案2】:

span.wlb_wrapper 是用于选择&lt;span class="wlb_wrapper" data-caller-name="search" data-size="small" data-tconst="tt0111161"&gt; 的选择器。有关选择器的更多信息,请参阅 thisthis

在你的 python 代码中将 span = td.select('span.wlb_wrapper') 更改为 span = td.select('span')span = td.select('span.year_type') 并查看它返回的内容。

如果你在上面尝试并分析span 持有什么,你会得到你想要的。

【讨论】:

  • 我已经编辑了正文以显示我试图在我的代码中执行的操作。我尝试将 span.wlb_wrapper 更改为 span 但它现在只返回一个值“None”
猜你喜欢
  • 2013-04-29
  • 1970-01-01
  • 2016-10-29
  • 2018-06-14
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多