2024-09-27 00:13:26 发布
网友
目前,我正在使用以下语法搜索元素:
priceele = soup.find(itemprop='price').string.strip()
实际上,该页面包含<span>元素,其属性名为itemprop,值为price。但是,我不需要寻找<span>元素,因为只有一个元素具有属性itemprop。在
<span>
itemprop
price
现在,我想把itemprop='price'作为一个变量传递给soup.find()方法,因为我是从数据库动态加载这两个东西的。有可能吗?在
itemprop='price'
soup.find()
如果通过“两件事”引用属性的名称和值,则可以通过使用**运算符应用任意关键字参数使它们成为动态的。例如:
**
attrname = 'itemprop' attrvalue = 'price' search = {attrname: attrvalue} priceele = soup.find(**search).string.strip()
如果通过“两件事”引用属性的名称和值,则可以通过使用
**
运算符应用任意关键字参数使它们成为动态的。例如:相关问题 更多 >
编程相关推荐