BeautifulSoup 在 <img alt 标签中查找文本 [英] BeautifulSoup to find text within <img alt tag
问题描述
这是我从这行 Python 代码中得到的结果
Here is what I get back from this Python line of code
listm = soup.findAll('td',{'class':'thumb'})
当我遍历 listm 时,这是一个项目的示例...
when I iterate over the listm, here is an example of a item...
<a href="/property-search/property-details/1021206?StrtNum=1507"><img alt="1507 BOSTWICK LN" src="/res/slir/w75-h57-c4:3/propertyimages/20120904/BB/DSCN0738.JPG"/></a>
然而,我真正想要的是 <img alt=
However, what I really want is the "1507 BOSTWICK LN" within the <img alt=
我试过 .get_text
返回空白,我试过了lista = soup.findAll('td',{'class':'thumb'},{'alt':'img'})
,以及其他一些无法获取文本的变体.
I have tried the .get_text
with returns a blank, I've tried
lista = soup.findAll('td',{'class':'thumb'},{'alt':'img'})
, and several other variations that do not get me the text.
我想让 BeautifulSoup 返回文本,还是使用正则表达式?我对 RE 的了解非常丰富,任何输入都将不胜感激!!
I would like to have BeautifulSoup return the text, or would a regular expression work? My knowledge of of RE is zilch, any input would greatly be appreciated!!
推荐答案
试试这个:
listm = soup.findAll('td',{'class':'thumb'})
for elem in listm:
print elem('img')[0]['alt']
这应该在每个 td
中找到 img
标签并打印 alt
属性的值.
This should find the img
tag within each td
and print the values of the alt
attribute.
您不应该假设 img
标签的存在.改为这样做:
You should not assume the existence of the img
tag. Do this instead:
listm = soup.findAll('td',{'class':'thumb'})
for elem in listm:
imgs = elem('img')
if imgs:
print imgs['alt']
这篇关于BeautifulSoup 在 <img alt 标签中查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!