本文实例讲述了python登录豆瓣并发帖的方法。分享给大家供大家参考。具体如下:
这里涉及urllib、urllib2及cookielib常用方法的使用
登录豆瓣,由于有验证码,采取的办法是将验证码图片下载到同目录下,查看图片后输入验证码即可登录、发帖
帖子内容写死在代码中了
# — coding:gbk —
import sys, time, os, re
import urllib, urllib2, cookielib
loginurl = ‘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” #没有的话登录不成功
}
#从首页提交登录
response=opener.open(loginurl, urllib.urlencode(params))
#验证成功跳转至登录页
if response.geturl() == “https://www.douban.com/accounts/login”:
html=response.read()
#验证码图片地址
imgurl=re.search(‘‘, html)
if imgurl:
url=imgurl.group(1)
#将图片保存至同目录下
res=urllib.urlretrieve(url, ‘v.jpg’)
#获取captcha-id参数
captcha=re.search(” ,html)
if captcha:
vcode=raw_input(‘请输入图片上的验证码:’)
params[“captcha-solution”] = vcode
params[“captcha-id”] = captcha.group(1)
params[“user_login”] = “登录”
#提交验证码验证
response=opener.open(loginurl, urllib.urlencode(params))
”’ 登录成功跳转至首页 ”’
if response.geturl() == “http://www.douban.com/”:
print ‘login success ! ‘
print ‘准备进行发帖’
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”] = ‘好了,发言’
request=urllib2.request(addtopicurl)
request.add_header(“user-agent”,”mozilla/5.0 (windows nt 6.1) applewebkit/536.11 (khtml, like gecko) chrome/20.0.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))
希望本文所述对大家的python程序设计有所帮助。