Attributeerror: 'nonetype' Object Has No Attribute 'lower' In Python 3.4
Code: import requests from bs4 import BeautifulSoup import operator def start(url): word_list =[] source_code =requests.get(url).text soup = BeautifulSoup(source_code)
Solution 1:
To understand what is going on, we should take a look at the documentation. There is a case when .string
would be None
:
If a tag contains more than one thing, then it’s not clear what .string should refer to, so .string is defined to be None
You should look into using get_text()
instead that would take into account the children of an element as well:
content = post_string.get_text()
Note that this would help to avoid the error, but you would still get no output since the element you find really does not have any text:
<atarget="_blank"href="Javascript:void(0)"class="cb-skin-ads-link cb-skin-ads-link-fixed ad-skin" ></a>
Solution 2:
For some reason, content is None. Try a debugger or print statements to see what you're getting from soup.find_all. It seems that the list isn't empty, but the string attribute of at least one element is None.
Post a Comment for "Attributeerror: 'nonetype' Object Has No Attribute 'lower' In Python 3.4"