When the a
element contains no text, eachsci.string
is None
- and you can't concatenate None
with a string using the +
operator, as you're trying to do.
If you replace eachsci.string
with eachsci.text
, that error is solved, because eachsci.text
contains the empty string ''
when the a
element is empty, and there's no problem concatenating that with another string.
However, you'll run into another problem when you hit an a
element with no href
attribute - when that happens, you'll get a KeyError
.
You can solve that using dict.get()
, which is able to return a default value if a key isn't in a dictionary (the a
element is pretending to be a dictionary, so this works).
Putting all that together, here's a variation on your for
loop that works:
for eachsci in sci:
print eachsci.get('href', '[no href found]') + "," + eachsci.text