简体   繁体   English

不清楚为什么我的函数不返回

[英]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! 除某些Ruby外,我的编码背景非常有限,因此,如果有更好的方法,请告诉我!

Essentially I have a .txt file full of words. 基本上,我有一个充满文字的.txt文件。 I want to import the .txt file and turn it into a list. 我想导入.txt文件并将其转换为列表。 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. 返回定义,并将其放入另一个.txt文件中。 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. 我了解到soup.find('pre', text=True)返回None ,但不是为什么或如何修复它。

your problem is that find() returns a single result not a list. 您的问题是find()返回单个结果而不是列表。 The result is a dict-like object so it tries to find the key 0 which it cannot. 结果是类似dict的对象,因此它尝试查找它不能找到的键0

just remove the [0] and you should be fine 只需删除[0] ,就可以了

Also soup.find(...) is not returning None . 而且soup.find(...)不会返回None It is returning an answer! 它正在返回答案! If it were returning None you would get the error 如果返回None ,则会出现错误

NoneType has no attribute __getitem__

Beautiful soup documentation for find() find()的漂亮汤文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM