# -*- coding: utf-8 -*-
#coding=utf-8 import urllib
import urllib2
import re
import thread
import time class QSBK:
def __init__(self):
self.pageIndex=1
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
self.header={'User-Agent':self.user_agent}
self.store=[]
self.enable=False
def getPage(self,pageIndex):
try:
url = 'http://www.qiushibaike.com/hot/page/'+str(pageIndex)
request = urllib2.Request(url,headers=self.header)
response = urllib2.urlopen(request)
pageHtml =response.read().decode('utf-8')
return pageHtml
except urllib2.URLError,e:
print '链接网络失败'+e.reason
return None
def getPageItem(self,pageIndex):
page = self.getPage(pageIndex)
if page==None:
print "页面获得失败"
return None
pattern = re.compile('<div class="author.*?<a.*?<img.*?</a>.*?<a.*?<h2>(.*?)</h2>.*?class="content.*?<span>\s*(.*?)\s*</span>',re.S)
items = re.findall(pattern, page)
pageStories = []
for item in items:
pageStories.append([item[0],item[1]])
return pageStories
def loadPage(self):
if self.enable==True:
if len(self.store)<2:
pageStories = self.getPageItem(self.pageIndex)
if pageStories!=None:
self.store.append(pageStories)
self.pageIndex+=1
def getOneStory(self,pageStories):
for story in pageStories:
input= raw_input()
self.loadPage()
if input=='Q':
self.enable=False
return
print u'%s %s'%(story[0],story[1])
def start(self):
print u"正在读取糗事百科的数据,按Q退出"
self.enable=True
self.loadPage()
nowPage=0
while self.enable:
if len(self.store)>0:
pageStore=self.store[0]
nowPage+=1
del self.store[0]
self.getOneStory(pageStore) spider =QSBK()
spider.start()

糗事百科python爬虫的更多相关文章

  1. Python 之糗事百科多线程爬虫案例

    import requests from lxml import etree import json import threading import queue # 采集html类 class Get ...

  2. [爬虫]用python的requests模块爬取糗事百科段子

    虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “HTTP for Humans”,说明使用更 ...

  3. python scrapy实战糗事百科保存到json文件里

    编写qsbk_spider.py爬虫文件 # -*- coding: utf-8 -*- import scrapy from qsbk.items import QsbkItem from scra ...

  4. 爬虫_糗事百科(scrapy)

    糗事百科scrapy爬虫笔记 1.response是一个'scrapy.http.response.html.HtmlResponse'对象,可以执行xpath,css语法来提取数据 2.提取出来的数 ...

  5. 利用python的爬虫技术爬去糗事百科的段子

    初次学习爬虫技术,在知乎上看了如何爬去糗事百科的段子,于是打算自己也做一个. 实现目标:1,爬取到糗事百科的段子 2,实现每次爬去一个段子,每按一次回车爬取到下一页 技术实现:基于python的实现, ...

  6. Python爬虫-爬取糗事百科段子

    闲来无事,学学python爬虫. 在正式学爬虫前,简单学习了下HTML和CSS,了解了网页的基本结构后,更加快速入门. 1.获取糗事百科url http://www.qiushibaike.com/h ...

  7. 8.Python爬虫实战一之爬取糗事百科段子

    大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...

  8. [Python]网络爬虫(八):糗事百科的网络爬虫(v0.2)源码及解析

    转自:http://blog.csdn.net/pleasecallmewhy/article/details/8932310 项目内容: 用Python写的糗事百科的网络爬虫. 使用方法: 新建一个 ...

  9. python网络爬虫--简单爬取糗事百科

    刚开始学习python爬虫,写了一个简单python程序爬取糗事百科. 具体步骤是这样的:首先查看糗事百科的url:http://www.qiushibaike.com/8hr/page/2/?s=4 ...

随机推荐

  1. 洛谷P1220 关路灯【区间dp】

    题目:https://www.luogu.org/problemnew/show/P1220 题意:给定n盏灯的位置和功率,初始时站在第c盏处. 关灯不需要时间,走的速度是1单位/秒.问把所有的灯关掉 ...

  2. Oracle下如何收集 Systemstate dump

    2: dump (不包括lock element) 10: dump 11: dump + global cache of RAC 256: short stack (函数堆栈) 258: 256+2 ...

  3. 前端-带header和footer的双栏布局

    目标是实现如上图带header和footer的双栏布局,其中右侧sidebar是固定宽度,左侧content是自适应: https://www.zybuluo.com/dengzhirong/note ...

  4. 第四篇、Swift_Podfile文件配置格式

    # Uncomment this line to define a global platform for your project platform :ios, '9.0' # Comment th ...

  5. webpack的配置处理

    1.webpack对脚本的处理 1.Js用什么loader加载? 1>webpack 本身就支持js的加载, 2>通过babel-loader ES2015 加载js,再用 babel-p ...

  6. music21 关联 MuseScore 和 Lilypond

    在python安装 music21后,需要关联 musescore 或 lilypond 才能可以用图形化的形式看到 乐谱. 因此 在安装 music21后,需要配置环境变量,yvivid 在 mus ...

  7. poj 1258 建光迁问题 最小生成树

    题意:给全村建光纤,求花费最小 思路:最小生成树,树相对于图来说就是没有环 m用来存图 v判断是否访问 low用来存两点间的最短距离 给low赋值  for(i=1;i<=n;i++){if(i ...

  8. Android引导页过多导致OOM内存泄漏

    摘要:前几天推广我们APP的时候,有些手机加载引导页的时候会闪退或崩溃,在Bugly显示是OOM异常.    然后Bugly上面显示的解决方案是: 该异常表示未能成功分配字节内存,通常是因为内存不足导 ...

  9. 【3Sum】cpp

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  10. html之表单标签

    表单标签的属性: 用于向服务器传输数据 表单能够包含input元素,比如文本字段,复选框,单选框,提交按钮等等 表单还可以包含textarea(简介之类的),select(下拉),fieldset和l ...