Drake Guan
Drake Guan

Reputation: 15152

Any Python modules to extract/suggest images for an URL?

I am thinking of trying to extend Pinry, a self-hostable Pinterest "clone". One of key features lack in Pinry is that it accepts image URLs only currently. I'm wondering if there is any suggested way to do that in Python?

Upvotes: 0

Views: 175

Answers (2)

aychedee
aychedee

Reputation: 25609

Yes there are lots of ways to do that, BeautifulSoup could be an option. Or even more simply you could grab the html with the requests library and then use a regex to match

<img src="">.

A full example using BeautifulSoup4 and requests is below

import requests
from bs4 import BeautifulSoup

r = requests.get('http://goodnewshackney.com')
soup = BeautifulSoup(r.text)

for img in soup.find_all('img'):
    print(img.get('src'))

Will print out:

http://24.media.tumblr.com/avatar_69da5d8bb161_128.png
http://24.media.tumblr.com/avatar_69da5d8bb161_128.png
....
http://25.media.tumblr.com/tumblr_m07jjfqKj01qbulceo1_250.jpg
http://27.media.tumblr.com/tumblr_m05s9b5hyc1qbulceo1_250.jpg

You will then need to present these images to the user somehow and let them pick one. Should be quite simple.

Upvotes: 2

Related Questions