# -*- 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. Activiti学习记录(四)

    1 连线 注意:如果将流程图放置在和java类相同的路径,需要配置: 1.1 部署流程定义+启动流程实例 ProcessEngine processEngine = ProcessEngines.ge ...

  2. oracle数据库删除表时遇见需要解锁问题

    今天在进行数据清空时,不注意把表锁住了,记录一下解锁过程. 第一步执行 select t2.username,t2.sid,t2.serial#,t2.logon_time from v$locked ...

  3. es6中的类及es5类的实现

    目录 类的特点 类的特点 1.类只能通过new得到 在es6中类的使用只能是通过new,如果你将它作为一个函数执行,将会报错. //es6的写法 class Child { constructor() ...

  4. java实现单链表归并算法

    public class LinkMergeSort {static int number=0;public static void main(String[] args) {int[] a = {1 ...

  5. I am too vegetable to all kill the 51nod problems on level 2 and 3.

    51nod level 2:50/51 剩的一个题是切比雪夫距离转曼哈顿距离,现学的,bzoj3710过了,51nod上全wa了,很迷,可能有坑⑧. level 3:62/68 之前有的题有思路但是不 ...

  6. A1043 Is It a Binary Search Tree (25 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  7. 1061: [Noi2008]志愿者招募

    Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 5742  Solved: 3449[Submit][Status][Discuss] Descript ...

  8. 2018.11.5 Nescafe26 T1 小猫爬山

    题目 题目描述 Freda 和 rainbow 饲养了 N 只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们 终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). Fre ...

  9. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...

  10. (80)zabbix性能优化中的几个建议

    随着zabbix的广泛应用,少数人的zabbix服务器在性能上出现瓶颈,或者在未来会出现性能方面的瓶颈,接下来讨论几个有效并且简单的优化方案. 服务器硬件 想通过几个简单的配置让服务器提高成倍的性能, ...