Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created August 25, 2012 15:14
Show Gist options
  • Save moluapple/3466953 to your computer and use it in GitHub Desktop.
Save moluapple/3466953 to your computer and use it in GitHub Desktop.
[Python2]使用 mechanize 登录网站演示
#!/usr/bin/python
# -*- coding:utf-8 -*-
import mechanize
def check_new_posts(username, password):
b = mechanize.Browser()
b._factory.encoding = "utf8"
b.set_handle_robots(False)
b.addheaders = [('User-Agent','Mozilla/4.0(compatible; MSIE 6.0; Windows 98;)')]
# 登录网站
b.open('http://www.cooldtp.cn/bbs/login.php?redirect=index.php')
b.select_form(nr=0)
b.form['username']=username
b.form['password']=password
b.submit()
# 检查新帖
b.open('http://www.cooldtp.cn/bbs/search.php?search_id=newposts')
if '没有符合您要求的主题或文章' in b.response().read():
print u'没有新主题或文章'
else:
print u'有新内容,请登录查看'
if __name__ == '__main__':
username='animalia'
password='********'
check_new_posts(username, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment