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的知识是zilch,任何输入将不胜感激!
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屋!