Question

Can someone tell me why this doesn't work?

import cookielib
import urllib
import urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
data = urllib.urlencode({'session[username_or_email]':'twitter handle' , 'session[password]':'password'})
opener.open('https://twitter.com' , data)
stuff = opener.open('https://twitter.com')
print stuff.read()

Why doesn't this give the html of the page after logging in?

Was it helpful?

Solution

Please consider using an Oauth library for your task. Scraping the site using mechanize is not recommended because twitter can change the HTML specific stuffs any time, and then your code will break.

Check this out: Python-twitter at http://code.google.com/p/python-twitter/

Simplest example to post an update:

>>> import twitter
>>> api = twitter.Api(
            consumer_key='yourConsumerKey',
            consumer_secret='consumerSecret',
            access_token_key='accessToken',
            access_token_secret='accessTokenSecret')
>>> api.PostUpdate('Blah blah lbah!')

There can be many reasons why it is failing:

  • Twitter probably expects a User-Agent header, which you are not providing.
  • I didn't look at the HTML, but many be there's some Javascript at play before the form is actually submitted (I actually think this is the case, because I vaguely remember writing a very detailed answer on this exact thing (and I dont seem to find the link to it!)).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top