PixelFallHD PixelFallHD - 3 months ago 28
Python Question

TypeError: argument of type 'NoneType' is not iterable in python?

The code below is a project I have been working on in my spare time. My friend owns a website and asked for me to make him a program to list all the php pages in his website. It worked on his website but as I tested on another website (http://lol.com) it didn't work. I searched stack overflow for an answer and it said something about cookies so I entered in the "hdr" part of my code. Now when I execute the program it doesn't work at all and says "line 20, in
if ("php" in PHPLink): TypeError: argument of type 'NoneType' is not iterable" Thanks for viewing my question.

import urllib2
from bs4 import BeautifulSoup

print("enter url: ")
url = str(raw_input(""))
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection' : 'keep-alive'}
request = urllib2.Request(url, headers=hdr)
website = urllib2.urlopen(request)
html = website.read()
tag = BeautifulSoup(html, "html.parser")
PHtml = tag.prettify()
print("PHP links:")
for link in tag.find_all('a'):
PHPLink = link.get('href')
if ("php" in PHPLink):
print(link.get('href'))
for link in tag.find_all('link'):
PHPLink = link.get('href')
if ("php" in PHPLink):
print(link.get('href'))

Answer

Let's look at:

 PHPLink = link.get('href')
 if ("php" in PHPLink):
     print(link.get('href'))

in tries to iterate over PHPLink. If the website doesn't contain any href tags then PHPLink will be None. And, as the error says, you can't iterate over None, ie NoneType is not iterable.

You should either add an if condition or use try-except. Since you already have an if condition you can change it to:

if PHPLink and "php" in PHPLink:

Now if PHPLink is None the second part of the if statement won't be evaluated (thanks to Python's short-circuiting) and no exception will be raised.

Comments