Python 将使用Selenium刮取的数据导出到csv文件
我正在尝试获取并将的首页中的数据导出为.csv文件。这是我目前的代码:Python 将使用Selenium刮取的数据导出到csv文件,python,selenium,web-scraping,Python,Selenium,Web Scraping,我正在尝试获取并将的首页中的数据导出为.csv文件。这是我目前的代码: import csv from selenium import webdriver def get_elements_by_xpath(driver, xpath): return [entry.text for entry in driver.find_elements_by_xpath(xpath)] url = ("https://icostats.com") driver = webdriver.Fire
import csv
from selenium import webdriver
def get_elements_by_xpath(driver, xpath):
return [entry.text for entry in driver.find_elements_by_xpath(xpath)]
url = ("https://icostats.com")
driver = webdriver.Firefox(executable_path=r'C:\Users\alph1\Scrapers\geckodriver.exe')
driver.get(url)
search_entries = [
("NAME", "//div[@class='tdName-0-73']"),
("DATE", "//div[@class='tdDate-0-74']"),
("CUR PRICE", "//div[@class='tdPrice-0-72'][1]"),
("ICO PRICE", "//div[@class='tdPrice-0-72'][0]"),
("24H ROI", "//div[@class='tdPrimary-0-75']")]
with open('textfile.csv', 'wb') as f_output:
csv_output = csv.writer(f_output)
# Write header
csv_output.writerow([name for name, xpath in search_entries])
entries = []
for name, xpath in search_entries:
entries.append(get_elements_by_xpath(driver, xpath))
csv_output.writerows(zip(*entries))
get_elements_by_xpath()
这是我得到的一个例外
csv_output.writerow([名称代表名称,搜索项中的xpath])中的第28行文件“C:/Users/alph1/PycharmProjects/PyQtPS/ICO2CSV.py”
TypeError:需要类似字节的对象,而不是“str”
我有一种感觉,我最终不应该这样调用该方法,但我不知道该怎么做。我相信这是因为您已将文件作为二进制文件打开(
open(
)。这意味着它的所有数据都将以二进制字节形式读取
要解决这个问题,只需执行
open('textfile.csv','w')
。看一看,可能会有所帮助。谢谢,但不确定它在我的情况下会如何应用。