【发布时间】:2019-08-07 04:33:30
【问题描述】:
我想从动态网站上的表格中检索所有信息,我有以下代码:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import sys
reload(sys)
import re
import csv
from time import sleep
sys.setdefaultencoding('utf-8') #added since it would give error for certain values when using str(i)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
prefs = {'profile.managed_default_content_settings.images':2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
maxcr = 1379
listofrows = []
url = "http://biggestbook.com/ui/catalog.html#/itemDetail?itemId=HERY4832YER01&uom=CT"
print(url)
driver.get(url)
wait = WebDriverWait(driver,10)
# Trying to get the table
tableloadwait = (wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".panel-body"))))
table = driver.find_elements_by_css_selector(".panel-body")
print(table)
RowsOfTable = table.get_attribute("tr")
但是,我不断收到错误消息,但到目前为止它不起作用。如何检索表的信息? 非常感谢!
错误: RowsOfTable = table.get_attribute("tr") AttributeError: 'list' 对象没有属性 'get_attribute'
【问题讨论】:
-
错误是什么,发生在哪里?
-
总是显示有问题的完整错误(Traceback)。
-
您将收到
AttributeError: 'list' object has no attribute 'get_attribute'错误,因为tr不是属性。你想从表中获取什么数据? -
find_elements_(sinelements) 总是给出包含许多元素的列表 - 所以你必须使用for循环来获取每个元素并单独使用get_attribute和每个元素. -
错误:RowsOfTable = table.get_attribute("tr") AttributeError: 'list' object has no attribute 'get_attribute'
标签: python json selenium web-scraping selenium-chromedriver