如何使用代理向twitter发送请求?

时间:2016-04-22 15:52:28

标签: twitter proxy tweepy

我正在尝试从twitter中提取数据。当我在家庭网络中使用代码时,它运行良好,但在我的大学,我们使用代理服务器。所以我需要配置请求。我不专攻计算机科学,所以我不知道如何修改请求。 我在https://dev.twitter.com/中阅读了文档,但我找不到解决方案!

这是代码的例子:

 import tweepy
 from tweepy import OAuthHandler

 consumer_key = 'XXXXXXXXXX'
 consumer_secret = 'XXXXXXXXXXXX'
 access_token = 'XXXXXXXXXXXXXXXXXXXX'
 access_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'

 auth = OAuthHandler(consumer_key, consumer_secret)
 auth.set_access_token(access_token, access_secret)

 api = tweepy.API(auth)

 for status in tweepy.Cursor(api.home_timeline).items(100):
     # Process a single status
       print(status.text) 

这是我得到的错误:

   Traceback (most recent call last):
    File "D:\formation\python\github tools\fbp.py", line 14, in <module>
   for status in tweepy.Cursor(api.home_timeline).items(100):
   File "C:\Anaconda\lib\site-packages\tweepy\cursor.py", line 197, in next
   self.current_page = self.page_iterator.next()
   File "C:\Anaconda\lib\site-packages\tweepy\cursor.py", line 108, in next
     data = self.method(max_id=self.max_id, parser=RawParser(), *self.args,             **self.kargs)
   File "C:\Anaconda\lib\site-packages\tweepy\binder.py", line 245, in _call
    return method.execute()
  File "C:\Anaconda\lib\site-packages\tweepy\binder.py", line 189, in execute
   raise TweepError('Failed to send request: %s' % e)
 TweepError: Failed to send request:    HTTPSConnectionPool(host='api.twitter.com', port=443): Max retries exceeded with   url: /1.1/statuses/home_timeline.json (Caused by ProxyError('Cannot connect to    proxy.', error('Tunnel connection failed: 407 Authentification proxy requise',)))

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码添加代理:

api =tweepy.API(auth,proxy="127.0.0.1:1080")

它适用于我的电脑。您可以尝试像这样修改代码。