繁体   English   中英

TypeError: 'WebElement' object 不可迭代

[英]TypeError: 'WebElement' object is not iterable

from selenium.webdriver.common.keys import Keys
import pandas as pd
from selenium.webdriver.common.by import By
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.investing.com/crypto/currencies")
elem = driver.find_element(By.TAG_NAME,'table')

head = elem.find_element(By.TAG_NAME,'thead')
body = elem.find_element(By.TAG_NAME,'tbody')

list_rows = []

for items in body.find_element(By.TAG_NAME,'tr'):
    list_cells = []
    for item in items.find_element(By.TAG_NAME,'td'):
        list_cells.append(item.text)
    list_rows.append(list_cells)
driver.close()

Output对于 body.find_element(By.TAG_NAME,'tr')中的项目:TypeError:'WebElement' object 不可迭代

我想从 selenium 和 pandas 的网站上抓取一张表格。但是我的 for 循环出现了一些错误。 请任何专家解决这个问题。 请给我一个代码,我可以用它来从任何网页的表格中抓取数据。

我的错误是body.find_element(By.TAG_NAME,'tr') 中的项目:TypeError: 'WebElement' object is not iterable

find_element返回单个 web 元素 object 而您需要获取 web 元素对象的列表。 find_elements这样做。
因此,您需要将表单find_element更改为find_elements ,这样它将是:

for items in body.find_elements(By.TAG_NAME,'tr'):
    list_cells = []
    for item in items.find_elements(By.TAG_NAME,'td'):
        list_cells.append(item.text)
    list_rows.append(list_cells)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM