I have an element that is returned in the [33] position with my code below.
<span>Beli 4 :<div currency-format="IDR" value="2500">Rp 2.500</div>/ pcs</span>
from https://alfagift.id/p/aice-mochi-ice-cream-klepon-45-ml-772306
I am trying to return Beli 4 : Rp 2.500 / pcs
However I have trouble doing so.
The code I have currently returns all span.
item = soup.select('span', {"currency-format":"USD"})
How do I get the span text and value in div class?
CodePudding user response:
Try this:
response = BeautifulSoup(driver.page_source, 'html.parser')
response.select_one("span[currency-format*=IDR]").text
CodePudding user response:
You could get the text by calling .text
but you should be aware, that you use select_one()
instead of select
, cause it could not be called on a ResultSet
and :
item = soup.select_one('span').text
There is also another issue, you seem to look for elements with USD, but there is only a IDS value, so more precise selection would be:
item = soup.select_one('span:has([currency-format="IDR"])').text
Just in case, if you have to work with multiple elements / ResultSet
you have to itrate it:
from bs4 import BeautifulSoup
html = '''
<span>Beli 4 :<div currency-format="IDR" value="2500">Rp 2.500</div>/ pcs</span>
<span>Beli 5 :<div currency-format="IDR" value="2500">Rp 2.500</div>/ pcs</span>
<span>Beli 6 :<div currency-format="IDR" value="2500">Rp 2.500</div>/ pcs</span>
'''
soup = BeautifulSoup(html)
for e in soup.select('span:has([currency-format="IDR"])'):
print(e.text)
Output:
Beli 4 :Rp 2.500/ pcs
Beli 5 :Rp 2.500/ pcs
Beli 6 :Rp 2.500/ pcs