newsAPI 导入错误,无法从 ‘newsapi’ 中导入 ‘NewsApiClient’。

huangapple go评论80阅读模式
英文:

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&#39;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 &#39;NewsApiClient&#39; from &#39;newsapi&#39; (c:\Users\tomh2\Desktop\newsapi.py)

(whole error)

Traceback (most recent call last):
  File &quot;c:\Users\tomh2\Desktop\news.py&quot;, line 2, in &lt;module&gt;
    from newsapi import NewsApiClient
ImportError: cannot import name &#39;NewsApiClient&#39; from &#39;newsapi&#39; (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 = (&#39;https://newsapi.org/v2/everything?&#39;
       &#39;q=Apple&amp;&#39;
       &#39;from=2023-06-11&amp;&#39;
       &#39;sortBy=popularity&amp;&#39;
       &#39;apiKey=0e24ed7870d04cc392d0a5804381faf7&#39;)

response = requests.get(url)

result = response.json()

print(result)

This should return the expected response.

huangapple
  • 本文由 发表于 2023年6月12日 18:59:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定