从Python爬虫到SAE云和微信公众号:一、糗事百科爬虫
2017-01-03 19:11
330 查看
这是写给自己玩的练习项目,从糗事百科中爬取段子放到微信公众号上去,这样我就能随时随地的看段子了,啊哈哈哈
2.用免费的新浪SAE云搭建微信公众号的服务器
3.微信公众号部分
页面解析代码
整个爬虫项目放到Github上吧,欢迎fork
GitHub地址:https://github.com/csdallen/qiushi_history_spider
参考资源:
Python开发简单爬虫
Python爬虫实战一之爬取糗事百科段子
项目结构
1.糗事百科爬虫:Pthon实现,MySQL做持久化存储2.用免费的新浪SAE云搭建微信公众号的服务器
3.微信公众号部分
# coding:utf8 import re class HtmlParser(object): #auther AllenRobin cnblogs.com/GISRSMAN/ # 将所有的段子都扣出来,添加到列表中并且返回列表 def __init__(self): self.storys=[] self.keywords=[] self.nextUrlCode='' #获得笑话正文、当前页Code、下一页Code def GetStorys(self,page): # print myPage unicodePage = page.decode("utf-8") # 找出所有class="content"的div标记 #re.S是任意匹配模式,也就是.可以匹配换行符 #myItems = re.findall('<div.*?class="content">(.*?)</div>',unicodePage,re.S) myItems = re.findall('<div.*?class="content">\n\n+<span>(.*?)</span>\n\n+</div>',unicodePage,re.S) thisUrlCode=re.findall('<link rel="canonical" href="http://www.qiushibaike.com/history/(.*?)/">',unicodePage,re.S)[0] nextUrlCode=re.findall('<a class="random" href="/history/(.*?)/".*?',unicodePage,re.S)[0] return myItems,thisUrlCode,nextUrlCode #获得笑话正文(作者赞数)、当前页Code、下一页Code def GetStorys2(self,pageContent): try: unicodePage= pageContent.decode("utf-8") pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?<div.*?' +'content">\n\n+<span>(.*?)</span>\n\n+</div>(.*?)<span class="stats-vote"><i class="number">(.*?)</i>',re.S) #items三个要素依次为用户名、段子内容、赞数 items = re.findall(pattern, pageContent) for item in items: #去除段子内容中的查看全文 item[1].replace("<span class=\"contentForAll\">查看全文","").replace("</span>","").replace("'","\"") #除去含有图片的 haveImg = re.search("img", item[3]) if haveImg: print item del item #可以将这三个合并到上一个提高效率 thisUrlCode = re.findall('<link rel="canonical" href="http://www.qiushibaike.com/history/(.*?)/">', unicodePage, re.S)[0] nextUrlCode = re.findall('<a class="random" href="/history/(.*?)/".*?', unicodePage, re.S)[0] dateStrs = re.findall('<meta name="keywords" content="(.*?)" />', unicodePage, re.S)[0] return items,thisUrlCode,nextUrlCode,dateStrs except Exception, e: print Exception, ":", e
页面解析代码
整个爬虫项目放到Github上吧,欢迎fork
GitHub地址:https://github.com/csdallen/qiushi_history_spider
参考资源:
Python开发简单爬虫
Python爬虫实战一之爬取糗事百科段子
相关文章推荐
- 从Python爬虫到SAE云和微信公众号:二、新浪SAE上搭建微信服务
- python爬虫(18)爬取微信公众号内容——绘制词云
- python scrapy爬取微信公众号文章的爬虫
- python爬虫_微信公众号推送信息爬取的实例
- Python 爬虫实例(15) 爬取 百度百聘(微信公众号)
- 基于Python实现微信公众号爬虫进行数据分析
- [Python爬虫] 之十五:Selenium +phantomjs根据微信公众号抓取微信文章
- Python实现微信公众号爬虫进行数据分析
- 基于Python实现微信公众号爬虫进行数据分析
- python爬虫(17)爬出新高度_抓取微信公众号文章(selenium+phantomjs)(上)
- Python 爬虫实战案例 : 微信公众号的爬取
- 【转帖】使用python爬虫抓站的一些技巧总结:进阶篇
- python网络爬虫
- python 爬虫
- Python学习笔记之网络爬虫
- Python写爬虫——抓取网页并解析HTML
- 【用Python写爬虫】获取html的方法【五】:利用Twisted框架之client.getPage
- 【转帖】用python爬虫抓站的一些技巧总结
- 07-爬虫的多线程调度 | 01.数据抓取 | Python
- 曲谱搜索进展 及 python的轻量级爬虫框架