【发布时间】:2014-08-12 20:45:06
【问题描述】:
我在使用 Python 2.7、beautifulsoup4 (4.2.1) 提取图像 src 时遇到困难。
我感兴趣的HTML部分是:
<div class="trb_embed_media "> <figure imgratio="16x9" imgwidth="750" imgheight="450" data-role="imgsize_item" class="trb_embed_imageContainer_figure"><img src="http://www.trbimg.com/img-53e8dc49/turbine/lat-buzzfeed-la0011761750-20131007/750/16x9" data-height="450" data-width="750" data-ratio="16x9" itemprop="image" data-baseurl="http://www.trbimg.com/img-53e8dc49/turbine/lat-buzzfeed-la0011761750-20131007" alt="Buzzfeed" class="trb_embed_imageContainer_img" title="Buzzfeed" data-content-naturalwidth="2048" data-content-naturalheight="1365"></figure><div class="trb_embed_related" data-role="lightbox_metadata"> <span class="trb_embed_related_title">Buzzfeed</span> <div class="trb_embed_related_credit">Jay L. Clendenin / Los Angeles Times</div> <div class="trb_embed_related_caption">Buzzfeed's Los Angeles headquarters on Beverly Boulevard on Oct. 7, 2013.</div> <div class="trb_embed_related_credit_and_caption">Buzzfeed's Los Angeles headquarters on Beverly Boulevard on Oct. 7, 2013. (Jay L. Clendenin / Los Angeles Times)</div></div> </div>
我正在运行的代码是:
image_section = soup.find(class_ = "trb_embed_media")
print image_section
print "================="
img = image_section.find('img')['src']
print img
上面代码第2行的输出是:
<div class="trb_embed_media ">
<figure class="trb_embed_imageContainer_figure" data-role=" delayload delayload_done imgsize_item">
<img alt="Buzzfeed" class="trb_embed_imageContainer_img" data-baseurl="http://www.trbimg.com/img-53e8dc49/turbine/lat-buzzfeed-la0011761750-20131007" data-content-naturalheight="1365" data-content-naturalwidth="2048" itemprop="image" title="Buzzfeed"/>
</figure>
<div class="trb_embed_related" data-role="lightbox_metadata">
<span class="trb_embed_related_title">
Buzzfeed
</span>
<div class="trb_embed_related_credit">
Jay L. Clendenin / Los Angeles Times
</div>
<div class="trb_embed_related_caption">
Buzzfeed's Los Angeles headquarters on Beverly Boulevard on Oct. 7, 2013.
</div>
<div class="trb_embed_related_credit_and_caption">
Buzzfeed's Los Angeles headquarters on Beverly Boulevard on Oct. 7, 2013. (Jay L. Clendenin / Los Angeles Times)
</div>
</div>
</div>
从上面的 img 标签可以看出。它缺少 src 属性,即使它存在于原始 HTML 源中。我在这里想念什么。请指教。
【问题讨论】:
标签: python html web-scraping beautifulsoup