python - BeautifulSoup 解析 Python

标签 python html web-scraping beautifulsoup

我使用 BS4 捕获了以下 html,但似乎无法搜索艺术家标签。 我已将此代码块分配给一个名为 container 的变量,然后尝试

print container.tr.td["artist"]

运气不好。 有什么建议吗?

<tr class="item">
  <!-- <td class="image"><a href="https://www.stargreen.com/kool-as-the-gang-44415.html" title="KOOL AS THE GANG " class="product-image"><img src="https://www.stargreen.com/media/catalog/product/cache/1/small_image/135x/9df78eab33525d08d6e5fb8d27136e95/K/o/KoolAsTheGang.jpg" width="135" height="135" alt="KOOL AS THE GANG " /></a></td> -->
  <td class="date">Sat, 30 Dec 2017</td>
  <td class="artist">kool as the gang</td>
  <td class="venue">100 club</td>
  <td class="link">
  <p class="availability out-of-stock">
    <span>Off Sale</span></p>
  </td>
</tr>

最佳答案

您的语法错误,“艺术家”是“类”属性的值试试这个:

from bs4 import BeautifulSoup

html = """
<tr class="item">
<!-- <td class="image"><a href="https://www.stargreen.com/kool-as-the-gang-44415.html" title="KOOL AS THE GANG " class="product-image"><img src="https://www.stargreen.com/media/catalog/product/cache/1/small_image/135x/9df78eab33525d08d6e5fb8d27136e95/K/o/KoolAsTheGang.jpg" width="135" height="135" alt="KOOL AS THE GANG " /></a></td> -->
<td class="date">Sat, 30 Dec 2017</td>
<td class="artist">
                        kool as the gang                     </td>
<td class="venue">100 club</td>
<td class="link">
<p class="availability out-of-stock">
<span>Off Sale</span></p>
</td>
</tr>
"""

soup = BeautifulSoup(html, 'html.parser')
td = soup.find('td',{'class': 'artist'})
print (td.text.strip())

输出:

kool as the gang

关于python - BeautifulSoup 解析 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48037685/

相关文章:

python - 更改编码python时数据从文件中消失

python - 如何将一个 .wav 文件分割成多个 .wav 文件?

python - "Teach"Python 其他语言(TTS 说话...)如何?

javascript - 使用 JavaScript 在 IE 中未正确设置禁用属性

python - 使用 scrapy 通过 javascript 按钮和 ajax 请求抓取 asp.net 网站

python - 通过组合文本 pandas 制作新的数据框

jquery - 使用全尺寸图像背景...固定元素到调整大小的图像?

javascript - 如果禁用 javascript,则让对话框链接转到 html 登录页面

python - 如何在 Python 中使用正则表达式从同一个字符串中提取多个值?

javascript - 寻找一种用 JS 抓取 HTML 的方法