【问题标题】:Python, BeautifulSoup - print only links which contains <img> in contentPython,BeautifulSoup - 仅打印内容中包含 <img> 的链接
【发布时间】:2013-08-02 13:27:04
【问题描述】:

是否可以在 beautifulSoup 中设置我只能打印内容中包含 &lt;img&gt; 的链接?

目前我的代码如下所示:

import urllib
import re
import mechanize
from bs4 import BeautifulSoup
import urlparse

url = "http://www.nytimes.com"

htmlcontent = urllib.urlopen(url)
soup = BeautifulSoup(htmlcontent)
for link in soup.find_all('a'):
    print link.contents

打印出链接内的所有内容。但我真正需要的是打印其中包含&lt;img&gt; 标签的链接内容,我不知道该怎么做......

欢迎任何帮助

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    尝试在链接中找到img标签:

    for link in soup.find_all('a'):
        if link.find('img'):
            print link
    

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多