ptwobrussell / Mining-the-Social-Web

The official online compendium for Mining the Social Web (O'Reilly, 2011)
http://bit.ly/135dHfs
Other
1.21k stars 490 forks source link

I can't get twitter trends in China #42

Closed hityang closed 11 years ago

hityang commented 11 years ago

My code is as follows . And I get the problem is some thing like net problems.

Traceback (most recent call last): File "C:\Python27\twitter1", line 27, in world_trends = twitter_api.trends.place(_id=1) File "C:\Python27\lib\site-packages\twitter-1.9.1-py2.7.egg\twitter\api.py", line 194, in call return self._handle_response(req, uri, arg_data, _timeout) File "C:\Python27\lib\site-packages\twitter-1.9.1-py2.7.egg\twitter\api.py", line 201, in _handle_response handle = urllib_request.urlopen(req, *_kwargs) File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 394, in open response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 412, in _open '_open', req) File "C:\Python27\lib\urllib2.py", line 372, in _call_chain result = func(_args) File "C:\Python27\lib\urllib2.py", line 1207, in https_open return self.do_open(httplib.HTTPSConnection, req) File "C:\Python27\lib\urllib2.py", line 1174, in do_open raise URLError(err) URLError: <urlopen error [Errno 10060] >

//codes-------------------------------------------------------------- import twitter import json

Go to http://twitter.com/apps/new to create an app and get these items

See https://dev.twitter.com/docs/auth/oauth for more information on Twitter's OAuth implementation

I have got these credentials.

CONSUMERKEY = '***'
CONSUMERSECRET = '**' OAUTHTOKEN = '**' OAUTH_TOKENSECRET = '***'

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(domain='api.twitter.com', api_version='1.1', auth=auth )

With an authenticated twitter_api in existence, you can now use it to query Twitter resources as usual.

However, the trends resource is cleaned up a bit in v1.1, so requests are a bit simpler than in the latest

printing. See https://dev.twitter.com/docs/api/1.1/get/trends/place

The Yahoo! Where On Earth ID for the entire world is 1

WORLD_WOE_ID = 1

Prefix id with the underscore for query string parameterization.

Without the underscore, it's appended to the URL itself

world_trends = twitter_api.trends.place(_id=1)

print json.dumps(world_trends, indent=1)

ptwobrussell commented 11 years ago

So just to be clear, are you saying that you can't run this sample code because you are in China and the government has blocked your Internet access to Twitter?

Regardless of the case, can you also paste in your stack trace that you get when running this example so that I can double check the kind of error you are receiving? Thanks.

hityang commented 11 years ago

This is my stack trace.Thanks very much.

Traceback (most recent call last): File "C:\Python27\twitter1", line 27, in world_trends = twitter_api.trends.place(_id=1) File "C:\Python27\lib\site-packages\twitter-1.9.1-py2.7.egg\twitter\api.py", line 194, in call return self._handle_response(req, uri, arg_data, _timeout) File "C:\Python27\lib\site-packages\twitter-1.9.1-py2.7.egg\twitter\api.py", line 201, in _handle_response handle = urllib_request.urlopen(req, *_kwargs) File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 394, in open response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 412, in _open '_open', req) File "C:\Python27\lib\urllib2.py", line 372, in _call_chain result = func(_args) File "C:\Python27\lib\urllib2.py", line 1207, in https_open return self.do_open(httplib.HTTPSConnection, req) File "C:\Python27\lib\urllib2.py", line 1174, in do_open raise URLError(err) URLError:

ptwobrussell commented 11 years ago

Can you also answer my other question? Are you suggesting that the reason you are getting this error is because of a government firewall or restriction?

For example, can you not access Twitter.com in your web browser?

On Mar 30, 2013, at 9:07 AM, hityang notifications@github.com wrote:

This is my stack trace.Thanks very much.

Traceback (most recent call last): File "C:\Python27\twitter1", line 27, in world_trends = twitter_api.trends.place(_id=1) File "C:\Python27\lib\site-packages\twitter-1.9.1-py2.7.egg\twitter\api.py", line 194, in call return self._handle_response(req, uri, arg_data, _timeout) File "C:\Python27\lib\site-packages\twitter-1.9.1-py2.7.egg\twitter\api.py", line 201, in _handle_response handle = urllib_request.urlopen(req, *_kwargs) File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 394, in open response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 412, in _open '_open', req) File "C:\Python27\lib\urllib2.py", line 372, in _call_chain result = func(_args) File "C:\Python27\lib\urllib2.py", line 1207, in https_open return self.do_open(httplib.HTTPSConnection, req) File "C:\Python27\lib\urllib2.py", line 1174, in do_open raise URLError(err) URLError:

— Reply to this email directly or view it on GitHub.

hityang commented 11 years ago

I can't access Twitter.com in my web browser unless I use ssh proxy or http proxy in my browser. Is there any way I can use to use proxy in python code when the code try to get twitter trends or other information.

hityang commented 11 years ago

Firstly, The code is ok? Secondly, If the government have blocked the access to twitter, what can I do ?Thanks for your help.

ptwobrussell commented 11 years ago

If you can get to twitter.com, then there's no reason that the code shouldn't work, but as you say, you'll have to configure and use your same proxy. It's going to take a few more questions to try and work through this with you, so bear with me while I try to figure out what you are doing.

What operating system are you using? Linux, Mac OS X, or Windows?

If you are running Linux or Mac OS X, are you savvy in the terminal? If so, have you tried setting the http_proxy and https_proxy environment variables to the settings you have in your browser? You should be able to know when you have this configured correctly by using a tool like curl to "curl http://twitter.com" through the terminal and getting back a good HTML page, for example.

Is this making sense? Let me know...

On Mar 30, 2013, at 10:08 PM, hityang notifications@github.com wrote:

Firstly, The code is ok? Secondly, If the government have blocked the access to twitter, what can I do ?Thanks for your help.

— Reply to this email directly or view it on GitHub.

hityang commented 11 years ago

Finally, I set http_proxy and https_proxy. It help me to run this code correctly.Thanks for your help. I woked in the windows os.

jakocoo commented 11 years ago

Using VPN would be a simple solution.

leithlee commented 11 years ago

example1-3 Hello,Please help me solve the problem! I also upload the problem photo! I come from China and I also meet the same problem. I use the goagent proxy in order to contact the twitter.And as a result,I just can explorer the Web page,but can not connect the twitter API.
This problem make me not continue to the following examples. Thank you very much for your reply! @hityang @ptwobrussell @jakocoo

hityang commented 11 years ago

Hello ,if you want to get twitter trends in China .you should set http_proxy and https_proxy in your enrionment varibles . But it is not always works;

What is more: your code should be like fllows: You can get the key and token from twitter web site .Please check how to get these informations ;

//----code --------------------------------------------------------------------------------------- import twitter import json

Go to http://twitter.com/apps/new to create an app and get these items

See https://dev.twitter.com/docs/auth/oauth for more information on Twitter's OAuth implementation

CONSUMERKEY = '*****' CONSUMERSECRET = '**' OAUTH_TOKEN = '*** OAUTH_TOKEN_SECRET = 'Z*'

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(domain='api.twitter.com', api_version='1.1', auth=auth )

With an authenticated twitter_api in existence, you can now use it to query Twitter resources as usual.

However, the trends resource is cleaned up a bit in v1.1, so requests are a bit simpler than in the latest

printing. See https://dev.twitter.com/docs/api/1.1/get/trends/place

The Yahoo! Where On Earth ID for the entire world is 1

WORLD_WOE_ID = 1

Prefix id with the underscore for query string parameterization.

Without the underscore, it's appended to the URL itself

world_trends = twitter_api.trends.place(_id=1) //-----------end of code ---------------------------------------------------------------

Third, don't worry the example , you can create a text which contains these informations ,and read them from the txt and do your example. And I think what is important in the Mining-the- socail-web is the idea about what information is import for us to analyse instead of the real detail inform. So, you can jump one or more steps and do the next examples. If you can get information from qq and renren web £¬that would be better .

At 2013-05-24 21:04:02,leithlee notifications@github.com wrote:

Hello,Please help me solve the problem! I also upload the problem photo! I come from China and I also meet the same problem. I use the goagent proxy in order to contact the twitter.And as a result,I just can explorer the Web page,but can not connect the twitter API.

This problem make me not continue to the following examples. Thank you very much for your reply! @hityang@ptwobrussell@jakocoo

¡ª Reply to this email directly or view it on GitHub.