英文:
newsAPI import error cannot import name 'NewsApiClient' from 'newsapi'
问题
我想使用NewsApi.org API制作一个新闻程序,但我遇到了一个我不知道如何解决的问题。
我的代码是:
```python
import requests
from newsapi import NewsApiClient
url = ('https://newsapi.org/v2/everything?'
'q=Apple&'
'from=2023-06-11&'
'sortBy=popularity&'
'apiKey=0e24ed7870d04cc392d0a5804381faf7')
response = requests.get(url)
r = 0
print (r.json)
而我得到的错误是:
ImportError: 无法从 'newsapi' 中导入名称 'NewsApiClient' (c:\Users\tomh2\Desktop\newsapi.py)
(完整错误信息)
Traceback (most recent call last):
File "c:\Users\tomh2\Desktop\news.py", line 2, in
from newsapi import NewsApiClient
ImportError: 无法从 'newsapi' 中导入名称 'NewsApiClient' (c:\Users\tomh2\Desktop\newsapi.py)
我希望获取最新的苹果新闻,但只得到了这个错误!
<details>
<summary>英文:</summary>
i wanted to make a program for news using the NewsApi.org Api, but i've got a problem i dont know how to solve.
my code is:
import requests
from newsapi import NewsApiClient
url = ('https://newsapi.org/v2/everything?'
'q=Apple&'
'from=2023-06-11&'
'sortBy=popularity&'
'apiKey=0e24ed7870d04cc392d0a5804381faf7')
response = requests.get(url)
r = 0
print (r.json)
and the error im getting is:
ImportError: cannot import name 'NewsApiClient' from 'newsapi' (c:\Users\tomh2\Desktop\newsapi.py)
(whole error)
Traceback (most recent call last):
File "c:\Users\tomh2\Desktop\news.py", line 2, in <module>
from newsapi import NewsApiClient
ImportError: cannot import name 'NewsApiClient' from 'newsapi' (c:\Users\tomh2\Desktop\newsapi.py)
i hoped to get the latest apple news, but just got this error!
</details>
# 答案1
**得分**: 0
您的主要脚本(c:\Users\tomh2\Desktop\news.py)无法导入`NewsApiClient`。似乎您甚至不需要这个类,因为它在您的主要脚本中没有被使用。请按以下方式更改您的`news.py`代码:
```python
import requests
url = ('https://newsapi.org/v2/everything?'
'q=Apple&'
'from=2023-06-11&'
'sortBy=popularity&'
'apiKey=0e24ed7870d04cc392d0a5804381faf7')
response = requests.get(url)
result = response.json()
print(result)
这应该返回预期的响应。
英文:
Your main script (c:\Users\tomh2\Desktop\news.py) cannot import NewsApiClient
. It seems that you do not even need this class, as it is not used in your main script. Change the code of your news.py
as follows:
import requests
url = ('https://newsapi.org/v2/everything?'
'q=Apple&'
'from=2023-06-11&'
'sortBy=popularity&'
'apiKey=0e24ed7870d04cc392d0a5804381faf7')
response = requests.get(url)
result = response.json()
print(result)
This should return the expected response.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论