我正在尝试导出表,但是我的代码导致错误: ValueError:找不到表。有人可以帮我吗?
以下代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import requests
import select
import pandas as pd
from bs4 import BeautifulSoup
browser = webdriver.Firefox()
browser.maximize_window()
browser.get('http://licytacje.komornik.pl/Notice/Search')
browser.find_element_by_xpath("//select[@name='Type']/option[text()='Nieruchomość']").click()
browser.find_element_by_class_name('button_next_active').click()
soup = BeautifulSoup(res.content,'lxml')
table = soup.find("table",{"class":"wMax"})
df = pd.read_html(str(table))
我改进了代码。现在如下所示:
import requests
import select
import pandas as pd
from bs4 import BeautifulSoup
browser = webdriver.Firefox()
browser.maximize_window()
browser.get('http://licytacje.komornik.pl/Notice/Search')
browser.find_element_by_xpath("//select[@name='Type']/option[text()='Nieruchomość']").click()
browser.find_element_by_class_name('button_next_active').click()
soup = BeautifulSoup(browser.page_source, "lxml")
table = soup.find("table",{"class":"wMax"})
df = pd.read_html(str(table))
writer = pd.ExcelWriter('test_licytacje.xlsx',engine='xlsxwriter')
df.to_excel(writer,sheet_name='New1',startrow=0 , startcol=0)[0]
但是当前代码的最后一行给出了这样的错误: AttributeError:“列表”对象没有属性“ to_excel”
任何反馈,请问出什么问题。
答案 0 :(得分:0)
问题是您的代码不符合逻辑:
df.to_excel(writer,sheet_name='New1',startrow=0 , startcol=0)[0]
read_html()
的{{3}}说它返回一个DataFrames列表。您正在尝试在此列表上调用to_excel()
。我想你想代替它:
df[0].to_excel(writer,sheet_name='New1',startrow=0 , startcol=0)