Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting HTML element value from a specific id via Python

So, I have a Bottle page within PythonAnywhere that is displaying a list of items in my SQLite database. In each of those tappable items, I want to give the other page relevant information from the database based on the name of the list item that was clicked.

So in this case, I want to grab the value of this element and use the variable in my SQLite query via Python.

<ons-list-item id="itembtn" name="itembtn" value="{{ record [0] }}" tappable>

However, it is to my knowledge that the value of an HTML element isn't possible to retrieve. I am wondering if anyone knows of a way to grab this dynamic value with Python.

<!-- Parts showing from database -->
        <template id="caseFanResult.html">
            <% result = c.execute("SELECT * FROM caseFanPart")%>
                <ons-page id="caseFanResult">
                    <ons-toolbar>
                        <div class="left">
                            <ons-back-button>Back</ons-back-button>
                        </div>
                        <div class="center">Case Fan Parts</div>
                    </ons-toolbar>
                    <ons-list modifier="material">
                        %for record in result:
                        <ons-list-item id="itembtn" name="itembtn" value="{{ record [0] }}" tappable>
                            <div class="center">
                                <span class="list-item__title">{{ record[1] }}</span>
                                <span class="list-item__subtitle">{{ record[2] }}</span>
                            </div>
                        </ons-list-item>
                        %end
                    </ons-list>
                </ons-page>
        </template>

        <!-- Parts detailed spec sheet -->
        <template id="itemResult.html">
            <% 
            id = request.form.get("itembtn","")
            itemResult = c.execute("SELECT * FROM caseFanPart WHERE caseFanID = ?", (id,))
            %>

                <ons-page id="itemResult">
                    <ons-toolbar>
                        <div class="left">
                            <ons-back-button>Back</ons-back-button>
                        </div>
                        <div class="center">Spec Sheet</div>
                    </ons-toolbar>
                    <ons-list modifier="material">
                        %for item in itemResult:
                        <ons-list-header>{{ item[1] }}</ons-list-header>
                        <ons-list-item>Price: {{ item[2] }}</ons-list-item>
                        <ons-list-item>Rating: {{ item[3] }}</ons-list-item>
                        <ons-list-item>Color: {{ item[4] }}</ons-list-item>
                        <ons-list-item>Size: {{ item[5] }}</ons-list-item>
                        <ons-list-item>RPM: {{ item[6] }}</ons-list-item>
                        <ons-list-item>Airflow: {{ item[7] }}</ons-list-item>
                        <ons-list-item>Noise Level:{{ item[8] }}</ons-list-item>
                        %end
                    </ons-list>
                </ons-page>
        </template>
like image 501
sch3p Avatar asked Dec 05 '25 04:12

sch3p


1 Answers

This is what you have to do to extract a value from an HTML element

import bs4 as bs

word = '<ons-list-item id="itembtn" name="itembtn" value="{{ record [0] }}" tappable>'

soup = bs.BeautifulSoup(word,'lxml')

supa = soup.find('ons-list-item',attrs={'id' : 'itembtn'})

value = supa.get('value')
#print(value)

#to find all values 
valuelist = []
supa = soup.find_all('ons-list-item',attrs={'id' : 'itembtn'})
for val in supa: 
    value = val.get('value')
    valuelist.append(value)
print(valuelist)
like image 120
Filip Lav Maksimovic Avatar answered Dec 07 '25 17:12

Filip Lav Maksimovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!