我有一个包含几个div.inventory
兄弟的文档。在
<div class="inventory">
<span class="item-number">123</span>
<span class="cost">
<a href="http://linktoitem" class="price">$1.23</a>
</span>
</div>
我想迭代它们以打印项目编号和项目链接。在
^{pr2}$在选择div.inventory
元素后,如何解析这两个值?在
import requests
from bs4 import BeautifulSoup
htmlSource = requests.get(url).text
soup = BeautifulSoup(htmlSource)
matches = soup.select('div.inventory')
for match in matches:
#prints 123
#prints http://linktoitem
另外,select
函数和find*
函数之间有什么区别?在
您可以使用依赖于} 找到这两个项目:
class
属性的^{示例:
^{pr2}$印刷品:
注意使用^{} -此方法允许使用} argument -下划线很重要,因为
CSS Selectors
在页面上搜索。还要注意使用^{class
在Python中是一个反向关键字。在相关问题 更多 >
编程相关推荐