【发布时间】:2021-11-17 21:20:00
【问题描述】:
我正在尝试运行以下 python 代码
技术:Python、Selenium 刮刀
设备:Windows 设备
出现错误......
Traceback (most recent call last):
File "scraper.py", line 35, in for row in cp_url:
ValueError: I/O operation on closed file.
#!/usr/bin/python3
# Description: The Python code below will search selenium in Google.
import time
import csv
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#EVERYTIME CHANGE THE DRIVER PATH TO THE CHROME DRIVER FOR LATEST CHROME VERSION
driver = webdriver.Chrome(
executable_path="D:\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
contents = []
filePath = 'output1.csv'
# As file at filePath is deleted now, so we should check if file
# exists or not not before deleting them
if os.path.exists(filePath):
os.remove(filePath)
else:
print("Can not delete the file as it doesn't exists")
f = open("output1.csv", "a")
f.write("website," + "htmltag," + "type," + "id," + "classname," + "for," + "href," + "alt," + "type," + "src,"
+ "name," + "width," + "height," + "data-src,"+ 'inner-text,' + 'action,' + 'value,' + "\n")
with open('inputLinks1.csv', 'rt') as cp_csv:
cp_url = csv.reader(cp_csv)
for row in cp_url:
links = row[0]
contents.append(links)
driver.get(links)
with open('xpathtags.csv', 'rt') as cp2_csv:
cp_url2 = csv.reader(cp2_csv)
for row1 in cp_url2:
print(row[0])
(xtype, xpathtext) = row1[0].split(';')
print(xtype, xpathtext)
contents.append(xtype)
contents.append(xpathtext)
elems = driver.find_elements_by_xpath(xpathtext)
for elem in elems:
f = open('output1.csv', 'a', encoding='utf-8')
f.write( links + ", "+ xtype + ","
+ str(elem.get_attribute('type')) + ', '
+ str(elem.get_attribute('id')) + ', '
+ str(elem.get_attribute('class')) + ', '
+ str(elem.get_attribute('for')) + ', '
+ str(elem.get_attribute('href')) + ', '
+ str(elem.get_attribute('alt')) + ', '
+ str(elem.get_attribute('type')) + ', '
+ str(elem.get_attribute('src')) + ', '
+ str(elem.get_attribute('name')) + ', '
+ str(elem.get_attribute('width')) + ', '
+ str(elem.get_attribute('height')) + ', '
+ str(elem.get_attribute('data-src')) + ', '
+ str(elem.get_attribute('innerText').strip()) + ', '
+ str(elem.get_attribute('action')) + ', '
+ str(elem.get_attribute('value')) + ', '
+ '\n')
f.close()
driver.close()
我正在使用以下 CSV 文件
A) inputlinks1.csv
www.flipkart.com
www.ebay.com
B) xpathtags.csv
Link;//a[@href]
Button;//button
Image;//img
Heading1;//h1
Heading2;//h2
Heading3;//h3
Heading4;//h4
C) Output.csv 是一个空白文件
我收到以下错误
Traceback (most recent call last):
File "scraper.py", line 35, in <module>
for row in cp_url:
ValueError: I/O operation on closed file.
【问题讨论】:
-
您在该行有语法错误,您是否使用了正确的间距而不是交换空格/制表符?
-
间距是正确的。可以请指导吗?
-
尝试将
with open('inputLinks1.csv', 'rt') as cp_csv:更改为with open('inputLinks1.csv', 'r') as cp_csv: -
谢谢Kamalesh。更改后我仍然遇到同样的错误。
-
这是完整的错误信息吗?始终显示完整的回溯。 Stackoverflow 不显示行号 - 哪一行是 35?
标签: python selenium csv web-scraping