其实是写Java的,但是最近学习Python,于是写了一个Python的简单脚本练手
如何找到SAE上面所有的精华帖子,周末一个人无聊于是研究了一下python的urllib2,下面说下自己收集精华帖子的思路:
1,发送相关模块的请求,生产html信息返回给本地
2,处理html信息,找到可以标示精华帖子的html
3,提取取出href熟悉和<a></a>标签包含的名称
思路很简单,代码也不复杂,下面帖上自己的代码
#! /usr/bin/env python #coding=utf-8 #Python 2.7 #author:tsface #date:2014-04-20 #since:1.0 import urllib2 import time import sys import re reload(sys) sys.setdefaultencoding('utf-8') #定义常量 BASE_PATH='http://cloudbbs.org/' _URL='http://cloudbbs.org/forum.php?mod=forumdisplay' headers ={'User_Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36'} pageSize=30 FIDS=[41,37,39,40,51,52,57,58,60,63,64,65,54,46,62] #定义一个用来函数用来读取当前页面的html文档 #since:1.0 def getHtml(url): realUrl = url consoleLog('request url: '+realUrl) req = urllib2.Request(realUrl,headers = headers) res = urllib2.urlopen(req) html = res.read().decode('utf-8').encode('gb2312','ignore'); return html #定义一个函数用来解析当前文档中的精华帖子的url,并且把帖子的类容存储到一个tuple中 def parseExcellentContent(html): if html: consoleLog('start parse DOM...') regDigest = '(<a[^>]*class="xst" >[\\d\\D]*?</a>)' #找到主题的正则表达是 regContent ='(<th[^>]*class="[\\d\\D].*?">[\\d\\D]*?</th>)' contentList = re.findall(regContent,html,re.S) digestList=[] for item in contentList: #找到精华帖子 _digest = re.findall('alt="digest"',item,re.S) if _digest: _digestContent=re.findall(regDigest,item,re.S) _href=re.findall('<a href="(.*?)"',_digestContent[0],re.S) _bbsName=re.findall('<a.*?>(.*?)</a>',_digestContent[0],re.S) digestList.append({"address":_href[0],"title":_bbsName[0]}) consoleLog('parse successfully...') return digestList else: consoleLog('Nothing to parse') #写信息到文件中 def writeInfo2File(fileName,info): _file=open(fileName,'w+') _file.write(info) _file.close() consoleLog(fileName+'has build...') #定义日志函数 def consoleLog(log): print '['+getCurrentFormatTime()+']'+log #获取本地格式化的时间 def getCurrentFormatTime(): return time.strftime("%Y-%m-%d %A %X %Z", time.localtime()) #定义替换特殊字符的函数 def replaceSpecial(_href): replacedUrl=_href replaceTab = [("<","<"),(">",">"),("&","&"),("&nbps;"," ")] for repl in replaceTab: replacedUrl=replacedUrl.replace(repl[0],repl[1]) return replacedUrl #获取SAE平台上的精华文章列表 def getSAEBBSExcellentInfo(): info="" for fid in FIDS: reqUrl = _URL+'&fid='+str(fid) for i in range(1,pageSize+1): _sigleReqUrl=reqUrl+'&page='+str(i) print _sigleReqUrl objs = parseExcellentContent(getHtml(_sigleReqUrl)) for obj in objs: info+=(BASE_PATH+replaceSpecial(obj['address']))+(" "*10)+obj['title']+'\n' info+=('='*50+str(fid)+'\n') writeInfo2File('test.txt',info) #测试 startTime = time.time() getSAEBBSExcellentInfo() endTime = time.time() consoleLog('the py spend '+str(endTime-startTime))
相关推荐
利用python 爬虫 爬取火车票的数据。值得学习一下!!!
python爬虫-爬取豆瓣音乐
python爬虫-爬取豆瓣评分前250的电影信息
Python爬虫 - 爬取百度百科页面.zip
Python爬虫 - 爬取新浪博客进行归档.zip
python爬虫 Python爬虫 - 爬取 Instagram 一个博主所有图片视频.zip
通过python爬虫采集城市的酒店数据 内容概要:使用python采集酒店数据 适用人群:做酒店数据市场调研,数据分析报告的人群 使用场景及目标:需要依靠python3环境,执行爬虫脚本 其他说明:需要使用开发者工具捕捉...
python爬虫-----爬取腾讯网上的图片jpg、png并下载到本地
python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧python爬虫,爬取贴吧...
python爬虫--爬取网站中的多个网页
初始python爬虫-爬取彼岸图单张到全部图片(csdn)————程序
python爬虫,爬取星巴克菜单商品所有图片以及名称
Python 实例 - 爬取某网站评论 Python源码Python 实例 - 爬取某网站评论 Python源码Python 实例 - 爬取某网站评论 Python源码Python 实例 - 爬取某网站评论 Python源码Python 实例 - 爬取某网站评论 Python源码...
Python爬虫实例-爬取豆瓣Top250-保存为表格
基于python语言开发、scrapy框架实现的博客园首页博客爬取项目 1、使用命令行创建项目 2、定义爬取的内容、标题 import scrapy class CnblogItem(scrapy.Item): title = scrapy.Field() #定义爬取的标题 link...
该程序通过分析youtobe红人信息的源码标签,获取网页信息,然后定向筛选;分页处理,连接跳转处理,访问异常处理
一个实战性项目,python入门 爬取百度百科python词条相关的1000个词条 也可以直接在我的博客里面查看:http://blog.csdn.net/qiqiyingse/article/details/51798833 解压密码:123 主要是之前写的内容因为百度百科的...
python爬虫,爬取糗事百科python爬虫,爬取糗事百科python爬虫,爬取糗事百科python爬虫,爬取糗事百科python爬虫,爬取糗事百科python爬虫,爬取糗事百科python爬虫,爬取糗事百科python爬虫,爬取糗事百科python...
Python应用实战代码-爬取综艺《哈哈哈哈哈》弹幕做情感分析
爬取过程的字典库需要两个条件,字库所对应的坐标与字典的svg矢量图的位置。然后对网站评论隐藏内容于字典库中的数据替换。解析所需要构造的内容如:头像、用户、标签、评论内容、图片、评分等。然后保存到txt或者...