python登录豆瓣并发帖的方法

yipeiwu_com6年前Python基础

本文实例讲述了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('<img id="captcha_image" src="(.+?)" alt="captcha" class="captcha_image"/>', html)
  if imgurl:
    url=imgurl.group(1)
    #将图片保存至同目录下
    res=urllib.urlretrieve(url, 'v.jpg')
    #获取captcha-id参数
    captcha=re.search('<input type="hidden" name="captcha-id" value="(.+?)"/>' ,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('<input type="hidden" name="topic_id" value="(.+?)">', html) 
        p["topic_id"] = m.group(1)
        m= re.search('<input type="hidden" name="topic_id_sig" value="(.+?)">', 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程序设计有所帮助。

相关文章

Python2/3中urllib库的一些常见用法

什么是Urllib库 Urllib是Python提供的一个用于操作URL的模块,我们爬取网页的时候,经常需要用到这个库。 升级合并后,模块中的包的位置变化的地方较多。 urllib库对照...

Django models.py应用实现过程详解

Django models.py应用实现过程详解

编写 models.py 文件 from django.db import models # Create your models here. class User_info(mod...

python事件驱动event实现详解

python事件驱动event实现详解

所有的计算机程序都可以大致分为两类:脚本型(单次运行)和连续运行型(直到用户主动退出)。 脚本型:脚本型的程序包括最早的批处理文件以及使用Python做交易策略回测等等,这类程序的特点是...

PyQt5下拉式复选框QComboCheckBox的实例

PyQt5下拉式复选框QComboCheckBox的实例

笔者在用PyQt5写GUI时碰到了需要使用下拉式复选框的情况,但是PyQt5中没有相应的组件,而网上找到的方法大多是qt使用的,所以不能直接拿来用。 没办法,在这种让人无奈的情况下,笔者...

python socket 超时设置 errno 10054

python socket.error: [Errno 10054] 远程主机强迫关闭了一个现有的连接。问题解决方案: 前几天使用python读取网页。因为对一个网站大量的使用urlop...