Josh
Josh

Reputation: 47

Not clear on why my function is returning none

I have very limited coding background except for some Ruby, so if there's a better way of doing this, please let me know!

Essentially I have a .txt file full of words. I want to import the .txt file and turn it into a list. Then, I want to take the first item in the list, assign it to a variable, and use that variable in an external request that sends off to get the definition of the word. The definition is returned, and tucked into a different .txt file. Once that's done, I want the code to grab the next item in the list and do it all again until the list is exhausted.

Below is my code in progress to give an idea of where I'm at. I'm still trying to figure out how to iterate through the list correctly, and I'm having a hard time interpreting the documentation.

Sorry in advance if this was already asked! I searched, but couldn't find anything that specifically answered my issue.

from __future__ import print_function
import requests
import urllib
from bs4 import BeautifulSoup

def get_definition(x):

    url = 'http://services.aonaware.com/DictService/Default.aspx?action=define&dict=wn&query={0}'.format(x)
    html = urllib.urlopen(url).read()
    soup = BeautifulSoup(html, "lxml")
    return soup.find('pre', text=True)[0]

lines = []
with open('vocab.txt') as f:
    lines = f.readlines()
lines = [line.strip() for line in lines]

definitions = []
for line in lines:
    definitions.append(get_definition(line))

out_str = '\n'.join(definitions)
with open('definitions.txt', 'w') as f:
    f.write(out_str)

the problem I'm having is

Traceback (most recent call last):
  File "WIP.py", line 20, in <module>
    definitions.append(get_definition(line))
  File "WIP.py", line 11, in get_definition
    return soup.find('pre', text=True)[0]
  File "/Library/Python/2.7/site-packages/bs4/element.py", line 958, in __getitem__
    return self.attrs[key]
KeyError: 0

I understand that soup.find('pre', text=True) is returning None, but not why or how to fix it.

Upvotes: 0

Views: 448

Answers (1)

esecules
esecules

Reputation: 357

your problem is that find() returns a single result not a list. The result is a dict-like object so it tries to find the key 0 which it cannot.

just remove the [0] and you should be fine

Also soup.find(...) is not returning None. It is returning an answer! If it were returning None you would get the error

NoneType has no attribute __getitem__

Beautiful soup documentation for find()

Upvotes: 2

Related Questions