糗事百科python爬虫
# -*- 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爬虫的更多相关文章
- Python 之糗事百科多线程爬虫案例
import requests from lxml import etree import json import threading import queue # 采集html类 class Get ...
- [爬虫]用python的requests模块爬取糗事百科段子
虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “HTTP for Humans”,说明使用更 ...
- python scrapy实战糗事百科保存到json文件里
编写qsbk_spider.py爬虫文件 # -*- coding: utf-8 -*- import scrapy from qsbk.items import QsbkItem from scra ...
- 爬虫_糗事百科(scrapy)
糗事百科scrapy爬虫笔记 1.response是一个'scrapy.http.response.html.HtmlResponse'对象,可以执行xpath,css语法来提取数据 2.提取出来的数 ...
- 利用python的爬虫技术爬去糗事百科的段子
初次学习爬虫技术,在知乎上看了如何爬去糗事百科的段子,于是打算自己也做一个. 实现目标:1,爬取到糗事百科的段子 2,实现每次爬去一个段子,每按一次回车爬取到下一页 技术实现:基于python的实现, ...
- Python爬虫-爬取糗事百科段子
闲来无事,学学python爬虫. 在正式学爬虫前,简单学习了下HTML和CSS,了解了网页的基本结构后,更加快速入门. 1.获取糗事百科url http://www.qiushibaike.com/h ...
- 8.Python爬虫实战一之爬取糗事百科段子
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
- [Python]网络爬虫(八):糗事百科的网络爬虫(v0.2)源码及解析
转自:http://blog.csdn.net/pleasecallmewhy/article/details/8932310 项目内容: 用Python写的糗事百科的网络爬虫. 使用方法: 新建一个 ...
- python网络爬虫--简单爬取糗事百科
刚开始学习python爬虫,写了一个简单python程序爬取糗事百科. 具体步骤是这样的:首先查看糗事百科的url:http://www.qiushibaike.com/8hr/page/2/?s=4 ...
随机推荐
- C的xml编程-libxml2
这里主要讲述libxml2在linux下的使用. (以下内容除了linux下的安装步骤是自己写的,其余均出自http://www.blogjava.net/wxb_nudt/archive/2007/ ...
- 垂直居中一个img
{ display:table-cell; text-align:center; vertical-align:middle; }
- SummerVocation_Learning--java的线程同步
public class Test_XCTB implements Runnable{ Timer timer = new Timer(); public static void main(Strin ...
- 2018.11.7 Nescafe29 T1 穿越七色虹
题目 题目背景 在 Nescafe27 和 28 中,讲述了一支探险队前往 Nescafe 之塔探险的故事…… 当两位探险队员以最快的时间把礼物放到每个木箱里之后,精灵们变身为一缕缕金带似的光,簇簇光 ...
- Nginx http重定向到https时,post请求变为get的问题
Http协议是有状态码的,比较熟悉的状态码是200,代表链接OK,还有404和403,一个是not found,另一个是forbidden.Not found不用多说,可能是地址栏输入有误,或者是网页 ...
- 绘制圆形 和 椭圆形:边圆形 imageellipse() 、 填充圆形imagefilledellipse()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...
- JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- 初学js之qq聊天实例
实现的功能为上图所示,但是每新发送的消息必须显示在最上面. 我实现了两版,样式有是一样的.我们直接看代码. 版本一: <!DOCTYPE html> <html lang=" ...
- FLASH、SDRAM
1.flash: 闪存,掉电之后里面的存储数据不会丢失,在嵌入式系统中用作存储Bootloader以及操作系统或者程序代码或者直接当硬盘使用(U盘).一般主要使用的FLASH有NOR flash和NA ...
- 求 1 到 n 的所有数的约数和
求 1 到 n 的所有数的约数和 暴力方法就是枚举每个数,算出他的约数和即可,这样有点慢. 另一种思路,枚举约数,判断他是谁的约数,并记录(即他的倍数有多少个),在乘以他自己. n/i求的是n以内,i ...