Answer the question
In order to leave comments, you need to log in
Why is it not iterable?
import requests
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r"путь")
driver.get("какой то сайт")
allurl = []
for i in driver.find_elements_by_tag_name('a'):
allurl.append(i.get_attribute('href'))
print(allurl) # выводит список как он есть
print(type(allurl)) # тип <class 'list'>
with open('url.csv', 'w', newline='') as fcsv:
writers = csv.writer(fcsv, delimiter=',')
for line in allurl:
writers.writerows(line)
driver.close()
writers.writerows(line)
"TypeError : 'NoneType' object is not iterable"?
Answer the question
In order to leave comments, you need to log in
Alexander , an error with Nonetype means that some of the found tags do <a>
not have href, as a result, None is written to the list, i.e.
This in itself would not be a problem in a list, but writerow assumes that each element is a list of data (a list of columns for csv), and you pass just a string, so it tries to iterate it (i.e. it will write rows letter by letter through delimiter). And it can no longer iterate None, and therefore swears.
You can fix it like this:
alllist = ['url1', 'url2', ..., None, ... ]
for line in allurl:
writers.writerow([line])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question