传入变量汤。找()方法beauthulsoup Python

2024-09-27 00:13:26 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正在使用以下语法搜索元素:

priceele = soup.find(itemprop='price').string.strip()

实际上,该页面包含<span>元素,其属性名为itemprop,值为price。但是,我不需要寻找<span>元素,因为只有一个元素具有属性itemprop。在

现在,我想把itemprop='price'作为一个变量传递给soup.find()方法,因为我是从数据库动态加载这两个东西的。有可能吗?在


Tags: 方法数据库元素string属性语法动态页面
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:26

如果通过“两件事”引用属性的名称和值,则可以通过使用**运算符应用任意关键字参数使它们成为动态的。例如:

attrname = 'itemprop'
attrvalue = 'price'
search = {attrname: attrvalue}

priceele = soup.find(**search).string.strip()

相关问题 更多 >

    热门问题