【问题标题】:Getting HTML element value from a specific id via Python通过 Python 从特定 id 获取 HTML 元素值
【发布时间】:2018-04-30 01:48:05
【问题描述】:

所以,我在 PythonAnywhere 中有一个 Bottle 页面,它显示了我的 SQLite 数据库中的项目列表。在每个可点击的项目中,我想根据被点击的列表项目的名称从数据库中为其他页面提供相关信息。

所以在这种情况下,我想通过 Python 获取这个元素的值并在我的 SQLite 查询中使用该变量。

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

但是,据我所知,无法检索 HTML 元素的值。我想知道是否有人知道用 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>

【问题讨论】:

    标签: python html bottle


    【解决方案1】:

    这是从 HTML 元素中提取值所必须执行的操作

    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)
    

    【讨论】:

    • 这似乎是一个了不起的想法,但循环遍历所有值似乎不起作用。目标是获取每条记录的值并将其用作 sqlite 查询的值。因此,当您单击一个列表项时,它会将其对应的值编号转移到下一页以显示相关信息。对此有何想法?
    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多