问题描述
我正在尝试以下代码:
import asyncio
import smmrpy
s = smmrpy.SMMRPY("286C6866B9")
URL = 'https://stackoverflow.blog/2018/09/13/ibm-and-stack-overflow-partner-to-support-ai-developers/'
async def main():
article = await s.get_smmry(URL)
print(article.content)
print(article.keywords)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
它总结了使用 SMMRY ( ) 和 asyncio(提供了 100 个请求的 API 密钥)的网站。 smmrpy 模块创建一个“文章”对象,虽然它可以打印属性,但我无法像往常一样将它们存储在变量/列表中,例如:
import asyncio
import smmrpy
s = smmrpy.SMMRPY("286C6866B9")
URL = 'https://stackoverflow.blog/2018/09/13/ibm-and-stack-overflow-partner-to-support-ai-developers/'
async def main():
article = await s.get_smmry(URL)
print(article.content)
print(article.keywords)
# option 1:
# content = article.content
# option 2:
# return article
# content = getattr(article, 'content')
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
我想做的是将属性值存储在变量中以供进一步导出。 关于我做错了什么的任何想法?
1楼
发现问题,问题在于变量范围。 函数内的变量是局部变量,不能全局调用,在函数内定义 this。 此块修复了问题并打印两次而没有错误:
import asyncio
import smmrpy
s = smmrpy.SMMRPY("286C6866B9")
URL = 'https://stackoverflow.blog/2018/09/13/ibm-and-stack-overflow-partner-to-support-ai-developers/'
async def main():
article = await s.get_smmry(URL)
global contents
contents = article.content
print(contents)
print(article.keywords)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print(contents)