Getting ValueError:基数为10的int()的文本无效,试图使用datanews模块



我试图使用Datanews API,但得到了ValueError。:-(

这是我的代码:

import datanews
datanews.api_key = '[API KEY]'
response = datanews.headlines(q='Apple', language=['en'])
articles = response['hits']
num = 0
for num in articles:
nnum = int(str(num))
if nnum != 10:
print(articles[nnum]['title'])
num = nnum + 1
else:
break

错误:

Traceback (most recent call last):
File "twitter.py", line 12, in <module>
nnum = int(str(num))
ValueError: invalid literal for int() with base 10: '{'url': 'https://www.theapplepost.com/2020/10/22/apple-discontinues-apple-tv-remote-app/', 'source': 'theapplepost.com', 'authors': ['The Apple Post'], 'title': "Apple discontinues Appl

当您尝试转换未格式化为整数的字符串值时,将引发PythonValueError: invalid literal for int() with base 10 error

要解决此问题,可以使用float()方法将字符串中的浮点数转换为整数。然后,您可以使用int()将您的数字转换为整数。

如果这不起作用,请确保字符串的值不包含任何字母。带字母的字符串不能转换为整数,除非这些字母在Python中有特殊含义。

为了正确地知道>gt;使用print语句调试

..............
for num in articles:
print(num) # This will tell you the truth
nnum = int(str(num))
..............
# if you have a list and you want to get index value pair
for i,v in enumerate(yourList)

我在您的输入中没有看到任何int'{'url': 'https://www.theapplepost.com/2020/10/22/apple-discontinues-apple-tv-remote-app/', 'source': 'theapplepost.com', 'authors': ['The Apple Post'], 'title': "Apple discontinues Appl

最新更新