请考虑:
<div class="someClass">
<a href="href">
<img alt="some" src="some"/>
</a>
</div>
我希望从一个图像(即img)标记中提取源(即src )属性,使用Beautiful Soup。我使用的是Beautiful Soup 4,无法使用a.attrs [ 'src']
来获取 src
,但可以获取 href
。我该怎么办?请考虑:
<div class="someClass">
<a href="href">
<img alt="some" src="some"/>
</a>
</div>
我希望从一个图像(即img)标记中提取源(即src )属性,使用Beautiful Soup。我使用的是Beautiful Soup 4,无法使用a.attrs [ 'src']
来获取 src
,但可以获取 href
。我该怎么办?你可以使用Beautiful Soup提取HTML img
标签的src属性。在我的示例中,htmlText
包含img
标签本身,但也可以与urllib2
一起用于URL。
对于URL:
from BeautifulSoup import BeautifulSoup as BSHTML
import urllib2
page = urllib2.urlopen('http://www.youtube.com/')
soup = BSHTML(page)
images = soup.findAll('img')
for image in images:
# Print image source
print(image['src'])
# Print alternate text
print(image['alt'])
对于带有img标签的文本
from BeautifulSoup import BeautifulSoup as BSHTML
htmlText = """<img src="https://src1.com/" <img src="https://src2.com/" /> """
soup = BSHTML(htmlText)
images = soup.findAll('img')
for image in images:
print(image['src'])
Python 3:
from bs4 import BeautifulSoup as BSHTML
import urllib
page = urllib.request.urlopen('https://github.com/abushoeb/emotag')
soup = BSHTML(page)
images = soup.findAll('img')
for image in images:
# Print image source
print(image['src'])
# Print alternate text
print(image['alt'])
如有需要,请安装模块
# Python 3
pip install beautifulsoup4
pip install urllib3
ID
不是image
标签的默认属性,因此您无法获取类似于image['id']
的任何内容。但是,如果您打印image
值,您将看到所有属性和值。也许您可以对其进行分词,并找到您要查找的具有该ID的图像。 - Abu Shoeb链接没有src
属性。您必须针对实际的img
标签。
import bs4
html = """<div class="someClass">
<a href="href">
<img alt="some" src="some"/>
</a>
</div>"""
soup = bs4.BeautifulSoup(html, "html.parser")
# this will return src attrib from img tag that is inside 'a' tag
soup.a.img['src']
>>> 'some'
# if you have more then one 'a' tag
for a in soup.find_all('a'):
if a.img:
print(a.img['src'])
>>> 'some'
以下是一种解决方法,即使img标签没有src属性也不会触发KeyError异常:
from urllib.request import urlopen
from bs4 import BeautifulSoup
site = "[insert name of the site]"
html = urlopen(site)
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img')
for img in images:
if img.has_attr('src'):
print(img['src'])
img
标签的src属性。在我的示例中,htmlText
包含了img
标签本身,但是它也可以与urllib2
一起用于URL。
Abu Shoeb的答案提供的解决方案在Python 3上不再起作用。以下是正确的实现方式:
对于URLs
from bs4 import BeautifulSoup as BSHTML
import urllib3
http = urllib3.PoolManager()
url = 'your_url'
response = http.request('GET', url)
soup = BSHTML(response.data, "html.parser")
images = soup.findAll('img')
for image in images:
# Print image source
print(image['src'])
# Print alternate text
print(image['alt'])
对于包含“img”标签的文本
from bs4 import BeautifulSoup as BSHTML
htmlText = """<img src="https://src1.com/" <img src="https://src2.com/" /> """
soup = BSHTML(htmlText)
images = soup.findAll('img')
for image in images:
print(image['src'])
a.attrs['src']
能够工作呢?在你展示的代码片段中并没有带有src
属性的<a>
标签。 - jwoddersrc
。还有其他问题吗? - iDelusionimg.attrs['src']
时也出错了。但后来我使用正则表达式得到了我想要的内容。 - iDelusion