1

HTML Code:

<td id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_tdBINPrice">
    <input id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_txtBuyItNowPrice" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Product_eBay1$txtBuyItNowPrice" 
    style="width:50px;" type="text" value="1435.97"/>                           
    <img id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_imgCustomPriceCalculator" src="/images/ChannelPriceCustom.png" style="width:16px;"/>
</td>

i would like to extra the text in 'Value' attribute ('1435.95')

i tried doing it by executing the following code, but no luck.

driver.get(someURL)
       page = driver.page_source
       soup = BeautifulSoup(page, 'lxml')
       price = soup.find('td', {'id' : re.compile('ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_tdBINPrice')})

       print(price)

Thank you!

2 Answers 2

2

Try the following code.

from bs4 import BeautifulSoup

html='''<td id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_tdBINPrice">
    <input id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_txtBuyItNowPrice" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Product_eBay1$txtBuyItNowPrice" 
    style="width:50px;" type="text" value="1435.97"/>                           
    <img id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_imgCustomPriceCalculator" src="/images/ChannelPriceCustom.png" style="width:16px;"/>
</td>'''

soup = BeautifulSoup(html, 'html.parser')

textval=soup.select_one("input[name='ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Product_eBay1$txtBuyItNowPrice']")
print(textval['value'])

OR

from bs4 import BeautifulSoup

html='''<td id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_tdBINPrice">
    <input id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_txtBuyItNowPrice" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Product_eBay1$txtBuyItNowPrice" 
    style="width:50px;" type="text" value="1435.97"/>                           
    <img id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_imgCustomPriceCalculator" src="/images/ChannelPriceCustom.png" style="width:16px;"/>
</td>'''

soup = BeautifulSoup(html, 'html.parser')

textval=soup.find("input" ,attrs={"name" : "ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Product_eBay1$txtBuyItNowPrice"})
print(textval['value'])
Sign up to request clarification or add additional context in comments.

Comments

2

There is an id which is the fastest selector. Use that to get the element then take the value attribute. Yours in part is failing as you are looking at td not input tag

from bs4 import BeautifulSoup as bs
html = '''
<td id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_tdBINPrice">
    <input id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_txtBuyItNowPrice" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Product_eBay1$txtBuyItNowPrice" 
    style="width:50px;" type="text" value="1435.97"/>                           
    <img id="ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_imgCustomPriceCalculator" src="/images/ChannelPriceCustom.png" style="width:16px;"/>
</td>
'''
soup = bs(html, 'lxml')
soup.select_one('#ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_txtBuyItNowPrice')['value']

Yours (with input):

print(soup.find("input", {"id": "ContentPlaceHolder1_ContentPlaceHolder1_Product_eBay1_txtBuyItNowPrice"})['value'])

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.