使用BeautifulSoup获取页面中所有图像的绝对路径列表

Mri*_*lla 0 python beautifulsoup

有人可以告诉我如何使用BeautifulSoup获取网页中所有图像的aboslute路径列表吗?

获取所有图像很简单.我这样做:

page_images = [image["src"] for image in soup.findAll("img")]
Run Code Online (Sandbox Code Playgroud)

......但是我在获得绝对路径方面遇到了困难.有帮助吗?

谢谢.

Man*_*dan 5

获得它们后,您必须规范化路径.这可以使用urlparse.urljoin.例如:

>>> urlparse.urljoin("http://google.com/some/path/", "../../img/icon.png")
'http://google.com/img/icon.png'
Run Code Online (Sandbox Code Playgroud)