第一次是按照https://blog.csdn.net/zhurui_idea/article/details/56481382的方法做的,出现了错误,经高人指点说是要用代理服务器,于是采用了如下的代码:
api = tweepy.API(auth, proxy="127.0.0.1:1080")
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)
这时报错是没有权限,我还是很开心的,因为总算连上了,查了document,原来我的Essential只能运行v2命令
我就查了V2版本的资料,按照推特机器人可以通过V2应用程序接口发布推文(或引用)吗?(基本访问权限) - 问答 - 云+社区 - 腾讯云重新写了代码
# 按照https://cloud.tencent.com/developer/ask/sof/262181的方法
client = tweepy.Client(bearer_token=bearer_token, consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret)
client.create_tweet(text="Yeah boy! I did it")
运行结果依然报错:
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 0x000001630E916B50>: 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: /2/tweets (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001630E916B50>: 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\Twitter4.py", line 11, in <module>
client.create_tweet(text="Yeah boy! I did it")
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\client.py", line 594, in create_tweet
return self._make_request(
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\client.py", line 118, in _make_request
response = self.request(method, route, params=request_params,
File "C:\Users\ampar\.virtualenvs\PyCharmLearningProject\lib\site-packages\tweepy\client.py", line 76, in request
with 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: /2/tweets (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001630E916B50>: 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'))
我感觉可能还是需要代理服务器,但是我不知道V2的代理服务器如何设置。
我用tweepy好几天了,一个东西都没做出来来,我想赶紧用tweepy发出我的第一个帖子,查找我的Media Studio里文件的media_ids。希望大家能帮帮我。
主要愿意是命令行没有走“上网加速”工具的的socks代理,你需要让命令行用本地代理,127.0.0.1就是你的本地IP。
如果是在命令行,输入如下代码,1080是你的端口号,你的“上网加速工具”里头可以查看。
set http_proxy=http://127.0.0.1:1080
set https_proxy=http://127.0.0.1:1080
如果在Python里头,添加如下代码,端口号同上。
import os
os.environ["http_proxy"] = "http://127.0.0.1:1231"
os.environ["https_proxy"] = "http://127.0.0.1:1231"
具体的信息参考如下两篇文章
这个需要特殊的代理工具,用工具代理即可,不用自己手动设置的,国外的网站一般都有这种问题
你感觉的应该是对的,涉及这方面的回答太直接了会被河蟹,找个服务器代理工具就行了
# 'x.x.x.x' = IP of squid server
your_squid_server = urllib2.ProxyHandler({'http': 'x.x.x.x', 'https': 'x.x.x.x'})
new_opener = urllib2.build_opener(your_squid_server)
urllib2.install_opener(new_opener)