我犯了那个错误?对我来说,它真的是隐藏和不明白
我的代码:
import tweepy
import random
import time
from random import shuffle
from pyshorteners.shorteners import Shortener
from bs4 import BeautifulSoup as bs4
import requests
def shortenerUrl(url):
myUrl = url
shortUrlSystem = Shortener('Osdb')
urlReturned = format(shortener.short(myUrl))
return urlReturned
i = random.randint(1, 144)
articleExtractor(i)
for i in listaCompleta:
api.update_status(status=i)
time.sleep(random.randint(247, 383))
我的错误:
Traceback (most recent call last):
File "mTArt.py", line 73, in <module>
articleExtractor(i)
File "mTArt.py", line 67, in articleExtractor
urlCorta = shortenerUrl(job_url)
File "mTArt.py", line 37, in shortenerUrl
urlReturned = format(shortener.short(myUrl))
NameError: name 'shortener' is not defined
那裁决是什么?你知道吗
shortener
未在代码中的任何位置定义。你知道吗可能您想使用
shortUrlSystem
,您在有错误的行上方定义了该行。你知道吗你应该写:
冗长的回答
在您的代码中,您正试图对名为
shortener
的对象调用方法short
:现在,python只知道导入的内容和定义的内容。你知道吗
您从模块
pyshorteners.shorteners
导入了Shortener
,但是python是区分大小写的,所以shortener
不是Shortener
您定义的之一是
shortUrlSystem
,因此python知道它,您将它定义为Shortener
对象,该对象恰好有一个short
方法(这意味着您可以对其调用short
)现在看一下我看到的文档中的示例
这里作者将
shortener
定义为Shortener
对象,因此命令shortener.short
是有效的。但是在您的代码中,由于shortener
没有定义,shortener.short
是无效的,您可以从发布的错误中看到:相关问题 更多 >
编程相关推荐