Reputation: 1817
Let's say I'm scraping a webpage, and I want to select a certain image on the webpage. Just as you can find elements based on their class name, I want to select an image by its src
tag. How would I select an image where I already know the src
tag?
i.e. I want to select the image whose src
tag is:
https://assets.bandsintown.com/images/pin.svg
Upvotes: 0
Views: 368
Reputation: 84465
You said a single image by its src
value. Use select_one
. Less work and you only need an attribute selector.
soup.select_one('[src="https://assets.bandsintown.com/images/pin.svg"]')['src']
Upvotes: 0
Reputation: 33384
Using Beautifulsoup you can do it many ways. You can use css selector , you can use regular expression as well.
Css Selector
for item in soup.select('img[src="https://assets.bandsintown.com/images/pin.svg"]'):
print(item['src'])
Regular Expression with find_all
import re
for item in soup.find_all('img',src=re.compile('https://assets.bandsintown.com/images/pin.svg')):
print(item['src'])
Upvotes: 0
Reputation: 193308
While @Blorgbeard's answer shows the Beautifulsoup
approach, using Selenium you can achieve the same using either of the following Locator Strategies:
css_selector
:
my_elements = driver.find_elements_by_css_selector("[src=\"https://assets.bandsintown.com/images/pin.svg\"]")
xpath
:
my_elements = driver.find_elements_by_xpath("//*[@src=\"https://assets.bandsintown.com/images/pin.svg\"]")
Upvotes: 1
Reputation: 103535
You can search by arbitrary attributes; this should work:
soup.findAll("img", {"src" : "https://assets.bandsintown.com/images/pin.svg"})
Upvotes: 5