A
A
Alexander2021-08-26 19:46:37
Python
Alexander, 2021-08-26 19:46:37

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()


Why error?
writers.writerows(line) 
"TypeError : 'NoneType' object is not iterable"?


Well, I'm running on an iterable object ... or I misunderstood something ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2021-08-27
@StupidQuestion

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])

You can immediately check that href exists so that it does not shove empty values ​​into the list.

S
sswwssww, 2021-08-26
@sswwssww

for line in allurl:
        writers.writerows(line)

replace with:
writers.writerows(allurl)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question