【发布时间】:2021-08-03 11:17:09
【问题描述】:
我有一张这样的桌子。我想获得特定课程的价值。例如,我只想要“模型”和“年份”类的值。
<table id="list_tbody">
<tr>
<th class="model">Ford</th>
<th class="year">2000</th>
<th class="colour">red</th>
</tr>
<tr>
<th class="model">Toyota</th>
<th class="year">2020</th>
<th class="colour">blue</th>
</tr>
<tr>
<th class="model">Audi</th>
<th class="year">2018</th>
<th class="colour">black</th>
</tr>
.
.
.
</table>
获取我使用的所有 tr;
cars=browser.find_elements_by_xpath('//table[@id="list_tbody"]/tr')
所以我所有的 tr 元素都在我的“汽车”列表中。为了得到它们中的每一个,我使用 for 循环
for car in cars: ...
我使用类似的东西,但它不起作用。
for car in cars:
carModel= car.find_element_by_xpath('//th[@class="model"]').text
carYear= car.find_element_by_xpath('//th[@class="year"]').text
print(f"Model : {carModel}, Year : {carYear}")
我想看看:
Model : Ford - Year : 2020
Model : Toyota - Year : 2010
. .
. .
. .
但它总是只返回列表的第一项。那么我怎样才能在元素列表中获得元素的特定部分?在使用 selenium 获取元素列表后,我还尝试使用 beautifulsoup 获得价值。但是这次我打字的时候
...
Carmodel=car.find("th" class_="model").text
它说“'WebElement' 对象没有属性 'find'”
【问题讨论】:
标签: python selenium web-scraping beautifulsoup