如何获取所有图像链接&使用python下载

时间:2014-08-31 10:21:10

标签: python image beautifulsoup

这是我的代码

from bs4 import BeautifulSoup
import urllib.request
import re

print("Enter the link \n")
link = input()

url = urllib.request.urlopen(link)
content = url.read()
soup = BeautifulSoup(content)
links = [a['href'] for a in soup.find_all('a',href=re.compile('http.*\.jpg'))]
print (len(links))
#print (links)
print("\n".join(links))

当我输入

http://keralapals.com/emmanuel-malayalam-movie-stills

我得到了输出

http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-0.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-1.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-2.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-3.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-4.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-5.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-6.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-7.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-8.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-9.jpg
http://keralapals.com/wp-content/uploads/2013/01/Emmanuel-malayalam-movie-mammootty-photos-pics-wallpapers-10.jpg

但是,当我提供输入时

http://www.raagalahari.com/actress/13192/regina-cassandra-at-big-green-ganesha-2014.aspx
or 
http://www.ragalahari.com/actress/13192/regina-cassandra-at-big-green-ganesha-2014.aspx

它不产生任何输出:(

所以,我需要获得其原始照片的链接。此页面只包含缩略图。当我们点击那些缩略图时,我们会得到原始图像链接。 我需要获取那些图像链接,需要下载:( 任何帮助都非常受欢迎.. :))

谢谢

Muneeb K

1 个答案:

答案 0 :(得分:3)

问题在于,在第二种情况下,以.jpg结尾的实际图片网址位于src标记的img属性中:

<a href="/actress/13192/regina-cassandra-at-big-green-ganesha-2014/image61.aspx">
    <img src="http://imgcdn.raagalahari.com/aug2014/starzone/regina-big-green-ganesha/regina-big-green-ganesha61t.jpg" alt="Regina Cassandra" title="Regina Cassandra at BIG Green Ganesha 2014">
</a>

作为一种选择,您也可以支持这种类型的链接:

links = [a['href'] for a in soup.find_all('a', href=re.compile('http.*\.jpg'))]

imgs = [img['src'] for img in soup.find_all('img', src=lambda x: x.endswith('.jpg'))]
links += imgs

print (len(links))
print("\n".join(links))

对于this url,它会打印:

http://imgcdn.raagalahari.com/aug2014/starzone/regina-big-green-ganesha/regina-big-green-ganesha61t.jpg
http://imgcdn.raagalahari.com/aug2014/starzone/regina-big-green-ganesha/regina-big-green-ganesha105t.jpg
http://imgcdn.raagalahari.com/aug2014/starzone/regina-big-green-ganesha/regina-big-green-ganesha106t.jpg
http://imgcdn.raagalahari.com/aug2014/starzone/regina-big-green-ganesha/regina-big-green-ganesha107t.jpg
...

请注意,我正在传递a function而不是正则表达式,我检查src属性是否以.jpg结尾。

希望它有所帮助,你今天学到了新东西。