您好,我是代码新手,我想将通过网站(editor.cnn.com)获得的数据另存为excel文件,但我没有得到想要的结果。
因为我想将所有“行”或“数据”水平保存在excel文件中。
这是我的代码。您能告诉我我必须更改或添加的代码吗?
提前谢谢!
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 bs4 import BeautifulSoup
import requests
import time
import pandas as pd
import os
from bs4 import BeautifulSoup as soup
from bs4 import NavigableString
import re
path = "/Users/Downloads/chromedriver.exe"
driver = webdriver.Chrome(path)
nl = []
driver.get("https://edition.cnn.com/")
driver.implicitly_wait(3)
html = driver.page_source
soup = BeautifulSoup(html, "lxml")
find_ingre = soup.select("div.cd__content")
for i in find_ingre:
nl.append(i.get_text())
import pandas as pd
from pandas import Series, DataFrame
df_4 = pd.DataFrame(nl)
df_4
答案 0 :(得分:0)
您可以使用openpxl而不是熊猫来创建Excel文件:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
# Append all results as row
ws.append(nl)
wb.save("yourfile.xlsx")