1. 提取某一页所有段子

# -*- coding:utf-8 -*-
import urllib
import urllib2
import re

page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'haha/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
    request = urllib2.Request(url,headers = headers)
    response = urllib2.urlopen(request)
    content = response.read().decode('utf-8')
    pattern=re.compile('h2>(.*?)</h2.*?<span>(.*?)</.*?number">(.*?)</.*?number">(.*?)<', re.S)
    items = re.findall(pattern,content)
    for item in items:
        print u"----------------------------------------\n发布人:%s内容:%s赞:%s\t评论数:%s\n"%(item[0],item[1],item[2],item[3])

except urllib2.URLError, e:
    if hasattr(e,"code"):
        print e.code
    if hasattr(e,"reason"):
        print e.reason

2.完善交互

按下回车,读取一个段子,显示出段子的发布人,内容,评论个数,点赞个数。

# -*- coding:utf-8 -*-
import urllib
import urllib2
import re
import thread
import time

class QSBK:
        def __init__(self):
                self.pageIndex = 1
                #初始化 ua
                self.user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
                #初始化 headers
                self.headers = { 'user-Agent' : self.user_agent }
                #存放段子
                self.stories = []
                #是否继续运行
                self.enable = False
        #根据索引获得页面代码
        def getPage(self,pageIndex):
                try:
                        url = 'http://www.qiu' \
                              'shibaike.com/hot/page' + str(pageIndex)
                        #构建请求request
                        request = urllib2.Request(url,headers = self.headers)
                        #利用urlopen获取页面代码
                        response = urllib2.urlopen(request)
                        #将页面转化为UTF-8编码
                        pageCode = response.read().decode('utf-8')
                        return pageCode
                except urllib2.URLError,e:
                        if hasattr(e,"reason"):
                            print u"连接失败,错误原因",e.reason
                            return None
        #根据页面代码,返回段子列表
        def getPageItems(self,pageIndex):
            pageCode = self.getPage(pageIndex)
            if not pageCode:
                print "页面加载失败。。。"
                return None
            pattern = re.compile('h2>(.*?)</h2.*?<span>(.*?)</.*?number">(.*?)</.*?number">(.*?)<', re.S)
            #pattern = re.compile('h2>(.*?)</h2.*?content">(.*?)</.*?number">(.*?)</', re.S)
            items = re.findall(pattern,pageCode)
            #用来存储每页的段子
            pageStories = []
            #遍历正则表达式匹配的信息
            for item in items:
                pageStories.append([item[0].strip(),item[1].strip(),item[2].strip(),item[3].strip()])
            return pageStories
        #加载并提取页面的内容,加入到列表中
        def loadPage(self):
            #如果当前未看的页数少于两页,则加载新一页
            if self.enable == True:
                if len(self.stories) < 2:
                    #获取新一页
                    pageStories = self.getPageItems(self.pageIndex)
                    #将该页面存放在全局list中
                    if pageStories:
                        self.stories.append(pageStories)
                        #页码索引加一
                        self.pageIndex += 1
        #调用该方法,每次回车输出一个段子
        def getOneStory(self,pageStories,page):
            #遍历
            for story in pageStories:
                #等待用户输入
                input = raw_input()
                #判断是否要加载新页面
                self.loadPage()
                #输入"quit"则程序结束
                if input == "quit":
                    self.enable =  False
                    return
                print u"第%d页\n----------------------------------------\n发布人:%s\n内容:%s\n赞:%s\t评论数:%s\n"%(page,story[0],story[1],story[2],story[3])
        #开始方法
        def start(self):
            print u"正在读取,回车查看新段子,quit退出"
            self.enable = True
            self.loadPage()
            nowPage = 0
            while self.enable:
                if len(self.stories) > 0:
                    pageStories = self.stories[0]
                    nowPage += 1
                    del self.stories[0]
                    self.getOneStory(pageStories,nowPage)


spider = QSBK()
spider.start()