[英]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.