Ampare1987 2022-03-03 22:28 采纳率: 55.6%
浏览 118
已结题

tweepy初学者hello world就出错,大家找找原因

问题遇到的现象和发生背景

第一次是按照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。希望大家能帮帮我。

  • 写回答

4条回答 默认 最新

  • Julian_0115 2022-03-05 20:40
    关注

    主要愿意是命令行没有走“上网加速”工具的的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"
    

    具体的信息参考如下两篇文章

    https://segmentfault.com/a/1190000039686752

    https://blog.csdn.net/whatday/article/details/112169945

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月5日
  • 已采纳回答 3月5日
  • 创建了问题 3月3日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作