帮助使用BeautifulSoup从表格单元格获取图像src

Bla*_*man 1 python beautifulsoup

所以我有一个html页面,它有一个表单,表格里面有一排产品.

我现在已经到了循环表行的地步,在每个循环中我抓住所有表格单元格.

for tr in t.findAll('tr'):
    td = tr.findAll('td')
Run Code Online (Sandbox Code Playgroud)

现在我想从第一个td抓取图像src url.

Html看起来像:

<tr>
  <td ...>
    <a href ... >
       <img ... src="asdf/asdf.jpg" .. >
    </a>
  </td>

  ...
</tr>
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我一直在考虑正则表达式.

我试过了:

td[0].a.image.src 但这没有用,因为它没有说'src'属性.

Ale*_*lli 6

使用

td[0].a.img['src']
Run Code Online (Sandbox Code Playgroud)

我想你在问题中使用imagefor img只是一个转录错误,但重要的一点是,在BeautifulSoup中,为了访问标签的HTML属性,你使用索引符号(就像['src']我上面的代码片段中所示),而不是dot-语法 - 点语法表示法实际上在树上向下进行(正如它在上面对两个点所做的​​那样,每个点都在之前a和之前img).