Python login and posting Method

Source: Internet
Author: User
This article mainly introduces how to log on to and post a message using python. It involves URL simulated login and cookie usage skills and has some reference value, for more information about how to log on to Douban using python and post it, see the example in this article. Share it with you for your reference. The details are as follows:

Use of urllib, urllib2, and cookielib

Log on to Douban. Because there is a verification code, you can download the verification code image to the same directory, view the image, and enter the verification code to log on and post it.

The post content is in the code.

# -- Coding: gbk -- import sys, time, OS, reimport urllib, urllib2, cookielibloginurl = 'https: // www.douban.com/accounts/login'cookie = cookielib. cookieJar () opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (cookie) params = {"form_email": "your email", "form_password": "your password", "source ": "index_nav" # unsuccessful logon if not found} # submit logon response = opener from the homepage. open (loginurl, urllib. urlencode (params) # if response. geturl () = "https://www.douban.com/accounts/login": html = response. read () # Verification Code image address imgurl = re. search ('', html) if imgurl: url = imgurl. group (1) # Save the image to the same directory res = urllib. urlretrieve (url, 'v.jpg ') # obtain the captcha-id parameter captcha = re. search ('', Html) if captcha: vcode = raw_input ('Enter the verification code on the image :') params ["captcha-solution"] = vcode params ["captcha-id"] = captcha. group (1) params ["user_login"] = "login" # submit the verification code for response = opener. open (loginurl, urllib. urlencode (params) ''' log on to the homepage ''' if response. geturl () = "http://www.douban.com/": print 'login success! 'Print 'preparing to post' p = {"ck": ""} c = [c. value for c in list (cookie) if c. name = 'ck '] if len (c)> 0: p ["ck"] = c [0]. strip ('"') addtopicurl =" http://www.douban.com/group/python/new_topic "res = opener. open (addtopicurl) html = res. read () m = re. search ('', Html) p ["topic_id"] = m. group (1) m = re. search ('', Html) p ["topic_id_sig"] = m. group (1) p ["rev_title"] = 'title' p ["rev_text"] = 'send body' p ["rev_submit"] = 'OK, speak 'request = urllib2.Request (addtopicurl) request. add_header ("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/4100.1132.57 Safari/536.11") request. add_header ("Accept-Charset", "GBK, UTF-8; q = 0.7, *; q = 0.3") request. add_header ("Origin", "http://www.douban.com") request. add_header ("Referer", "http://www.douban.com/group/python/new_topic") opener. open (request, urllib. urlencode (p ))

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.