How to get attribute value from li tag in python BS4

Question:

How can I get the src attribute of this link tag with BS4 library?

Right now I’m using the code below to achieve the resulte but i can’t

<li class="active" id="server_0" data-embed="<iframe src='https://vk.com/video_ext.php?oid=757563422&id=456240701&hash=1d8fcd32c5b5f28b' scrolling='no' frameborder='0' width='100%' height='100%' allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' ></iframe>"><a><span><i class="fa fa-eye"></i></span> <strong>vk</strong></a></li>

i want this value src=’https://vk.com/video_ext.php?oid=757563422&id=456240701&hash=1d8fcd32c5b5f28b’

this my code i access [‘data-embed’] i don’t how to exract the link this my code

from bs4 import BeautifulSoup as bs
import cloudscraper

scraper = cloudscraper.create_scraper()
access = "https://w.mycima.cc/play.php?vid=d4d8322b9"
response = scraper.get(access)
doc2 = bs(response.content, "lxml")
container2 = doc2.find("div", id='player').find("ul", class_="list_servers list_embedded col-sec").find("li")
link = container2['data-embed']
print(link)

Result

<Response [200]>
https://w.mycima.cc/play.php?vid=d4d8322b9
<iframe src='https://vk.com/video_ext.php?oid=757563422&id=456240701&hash=1d8fcd32c5b5f28b' scrolling='no' frameborder='0' width='100%' height='100%' allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' ></iframe>

Process finished with exit code 0
Asked By: unees kiba

||

Answers:

From the beautiful soup documentation

You can access a tag’s attributes by treating the tag like a
dictionary

They give the example:

tag = BeautifulSoup('<b id="boldest">bold</b>', 'html.parser')
tag['id']
# 'boldest'

Reference and further details,
see: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes

So, for your case specifically, you could write

print(link.find("iframe")['src'])

if link turns out to be plain text, not a soup object – which may be the case for your particular example based on the comments – well then you can resort to string searching, regex, or more beautiful soup’ing, for example:

link = """<Response [200]>https://w.mycima.cc/play.php?vid=d4d8322b9<iframe src='https://vk.com/video_ext.php?oid=757563422&id=456240701&hash=1d8fcd32c5b5f28b'></iframe>"""
iframe = re.search(r"<iframe.*>", link)
if iframe:
    soup = BeautifulSoup(iframe.group(0),"html.parser")
    print("src=" + soup.find("iframe")['src'])
Answered By: topsail