1

I'm trying to extract image URLs from this code:

<div class="theme-screenshot one attachment-theme-screenshot size-theme-screenshot wp-post-image loaded" data-featured-src="https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg" data-src="https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg" style='background-image: url("https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg");'></div>

How can I find the URLs in data-src?

I'm using beautiful soup and find function but I have no idea how to extract links because I don't see img tag as usual...

Thank you for your time in advance

1
  • You are trying to find the value of data-src attribute of this div tag. Now, you only need to search what I just now told you, pretty sure it's already answered several times in other threads. Commented Jul 29, 2022 at 15:59

2 Answers 2

1

If you can't use an HTML parser for whatever reason, then you can use regex.

import re

text = '''
<div class="theme-screenshot one attachment-theme-screenshot size-theme-screenshot wp-post-image loaded" data-featured-src="https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg" data-src="https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg" style='background-image: url("https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg");'></div>
'''

parsed = re.search('(?<=data-src=").*(?=" )', text).group(0)

print(parsed)
Sign up to request clarification or add additional context in comments.

Comments

1

You can try the following:

from bs4 import BeautifulSoup

html = """
<div class="theme-screenshot one attachment-theme-screenshot size-theme-screenshot wp-post-image loaded" data-featured-src="https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg" data-src="https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg" style='background-image: url("https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg");'></div>
"""
soup = BeautifulSoup(html, "html.parser")
url = soup.select_one(
    "div.theme-screenshot.one.attachment-theme-screenshot.size-theme-screenshot.wp-post-image.loaded"
).get("data-src")

print(url)

This will return:

https://websitedemos.net/wp-content/uploads/2019/07/outdoor-adventure-02-home.jpg

Documentation for BeautifulSoup(bs4) can be found at:

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.