{ "metadata": { "name": "hw.4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CC98\u81ea\u52a8\u76d6\u697c\n", "\n", "\u4fee\u6539\u4e0b\u9762\u7684\u4ee3\u7801, \u5b9e\u73b0`content`\u6210\u5458\u51fd\u6570. \u8981\u6c42`content`\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32, \u5b57\u7b26\u4e32\u5305\u542b\u4e24\u90e8\u5206:\n", "\n", "1. \u4f60\u7684\u56de\u5e16\u6240\u5728\u7684\u697c\u5c42.\n", "2. \u4f60\u7684\u90ae\u7bb1\u7684sha1 hash\u768416\u8fdb\u5236\u8868\u793a.\n", "\n", "\u4e24\u90e8\u5206\u4e4b\u95f4\u7528\u4e00\u4e2a\u7a7a\u683c\u5206\u9694.\n", "\n", "\u5176\u4e2d`password`\u6587\u672c\u6587\u4ef6\u91cc\u4fdd\u5b58\u7684\u662f\u4f60\u81ea\u5df1\u7684\u5bc6\u7801, \u4fdd\u5b58\u5728\u672c\u5730\u5373\u53ef. \u5982\u679c\u4f60\u5e0c\u671b\u81ea\u5df1\u7684\u7528\u6237\u540d\u548c\u90ae\u7bb1\u4fdd\u5bc6, \u4e5f\u53ef\u4ee5\u628a\u7528\u6237\u540d\u548c\u90ae\u7bb1\u4e5f\u4fdd\u5b58\u5728\u672c\u5730\u6587\u4ef6\u91cc, \u7136\u540e\u8bfb\u53d6\u8fdb\u6765.\n", "\n", "## \u8981\u6c42\n", "\n", "* \u697c\u5c42\u5fc5\u9700\u8ba9\u7a0b\u5e8f\u81ea\u52a8\u53bb\u8ba1\u7b97, \u4e0d\u80fd\u5199\u6b7b\u5728\u4ee3\u7801\u91cc.\n", "* \u63d0\u4ea4\u7684gist\u91cc\u5fc5\u9700\u5305\u542b\u7a0b\u5e8f\u7684\u8f93\u51fa.\n", "\n", "## \u63d0\u793a\n", "\n", "* `hashlib.sha1(email).hexdigest()`\n", "* [CC98\u65b0\u5e16\u63d0\u9192](http://nbviewer.ipython.org/urls/raw.github.com/zhangxaochen/python-course.2014/master/noti98.ipynb)\n", "* [CC98\u81ea\u52a8\u56de\u590d](http://nbviewer.ipython.org/urls/raw.github.com/zhangxaochen/python-course.2014/master/reply98.ipynb)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import re\n", "import bs4\n", "import urllib\n", "import urllib2\n", "from cookielib import CookieJar\n", "\n", "login_url = 'http://www.cc98.org/login.asp'\n", "reply_url = 'http://www.cc98.org/SaveReAnnounce.asp'\n", "\n", "class Replyer(object):\n", " \n", " def __init__(self, username, password):\n", " self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))\n", " self.opener.open(login_url, urllib.urlencode(dict(\n", " username=username,\n", " password=password,\n", " action='chk'\n", " )))\n", "\n", " def info(self, url):\n", " soup = bs4.BeautifulSoup(self.opener.open(url).read(), 'lxml')\n", " keys = ('RootID', 'followup', 'UserName', 'passwd', 'star', 'signflag')\n", " d = {key: soup.find(attrs={'name': key})['value'] for key in keys}\n", " d['method'] = 'fastreply'\n", " d['Expression'] = 'face7.gif'\n", " d['BoardID'] = re.search(r'BoardID=(\\d+)', soup.find(attrs={'name': 'frmAnnounce'})['action']).group(1)\n", " return d\n", " \n", " def reply(self, url):\n", " d = self.info(url)\n", " d['Content'] = self.content(url)\n", " data = self.opener.open(urllib2.Request(\n", " reply_url, \n", " headers={'Referer': 'http://www.cc98.org'}, \n", " data=urllib.urlencode(d)\n", " )).read()\n", " return d['Content']\n", " \n", " def content(self, url):\n", " pass\n", "\n", "with open('password') as f:\n", " password = f.read().strip()\n", " \n", "r = Replyer('username', password)\n", "print r.reply('http://www.cc98.org/dispbbs.asp?boardID=509&ID=4223610&page=1')" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }