问题遇到的现象和发生背景
我按照https://blog.csdn.net/zhurui_idea/article/details/56481382的方法,写了第一段代码
问题相关代码,请勿粘贴截图
#导入tweepy
import tweepy
#填写twitter提供的开发Key和secret
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
#提交你的Key和secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#获取类似于内容句柄的东西
api = tweepy.API(auth)
#打印我自己主页上的时间轴里的内容
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
运行结果及报错内容
在PyCharm中运行,报错如下
C:\Users\ampar\.virtualenvs\PyCharmLearningProject\Scripts\python.exe "I:/Hands-On Machine Learning with Scikit-Lean/Twitter2.py"
Traceback (most recent call last):
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
raise err
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connection.py", line 358, in connect
conn = self._new_conn()
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000026EA87B1190>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\adapters.py", line 440, in send
resp = conn.urlopen(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with url: /1.1/statuses/update.json?status=hello+python (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026EA87B1190>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\api.py", line 216, in request
resp = self.session.request(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with url: /1.1/statuses/update.json?status=hello+python (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026EA87B1190>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "I:\Hands-On Machine Learning with Scikit-Lean\Twitter2.py", line 13, in <module>
api.update_status('hello python')
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\api.py", line 46, in wrapper
return method(*args, **kwargs)
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\api.py", line 1121, in update_status
return self.request(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\api.py", line 222, in request
raise TweepyException(f'Failed to send request: {e}').with_traceback(sys.exc_info()[2])
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\api.py", line 216, in request
resp = self.session.request(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\requests\adapters.py", line 519, in send
raise ConnectionError(e, request=request)
tweepy.errors.TweepyException: Failed to send request: HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with url: /1.1/statuses/update.json?status=hello+python (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026EA87B1190>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
Process finished with exit code 1
请问是什么原因,如何解决,谢谢大家!