pyshortener错误无法辨认,

2024-10-04 03:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我犯了那个错误?对我来说,它真的是隐藏和不明白

我的代码:

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

那裁决是什么?你知道吗


Tags: infrompyimporturl错误linerandom
1条回答
网友
1楼 · 发布于 2024-10-04 03:19:46

shortener未在代码中的任何位置定义。你知道吗

可能您想使用shortUrlSystem,您在有错误的行上方定义了该行。你知道吗

你应该写:

urlReturned = format(shortUrlSystem.short(myUrl))

冗长的回答

在您的代码中,您正试图对名为shortener的对象调用方法short

urlReturned = format(shortUrlSystem.short(myUrl))

现在,python只知道导入的内容和定义的内容。你知道吗

您从模块pyshorteners.shorteners导入了Shortener,但是python是区分大小写的,所以shortener不是Shortener

定义的之一是shortUrlSystem,因此python知道它,您将它定义为Shortener对象,该对象恰好有一个short方法(这意味着您可以对其调用short

现在看一下我看到的文档中的示例

url = 'http://www.google.com'
shortener = Shortener('Osdb')
print "My short url is {}".format(shortener.short(url))

这里作者shortener定义为Shortener对象,因此命令shortener.short是有效的。但是在您的代码中,由于shortener没有定义,shortener.short是无效的,您可以从发布的错误中看到:

NameError: name 'shortener' is not defined

相关问题 更多 >