首页
社区
课程
招聘
[原创]python获取QQ空间前100篇blog的地址和标题的方法
2010-12-15 00:43 7569

[原创]python获取QQ空间前100篇blog的地址和标题的方法

2010-12-15 00:43
7569
相关地址均由firebug获得,本来想把QQ空间的日志全部下载下来,可是目前只能获取前100篇的文章,先发出个半成品
#! /usr/bin/env python   
#coding=utf-8   
import urllib2,urllib, os
from HTMLParser import HTMLParser

QQnum='1026239701'
loginurl='http://g.cnc.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin='+QQnum+'&loginUin=0&s=414636&num=3&g_tk=5381'
blogurl='http://br.cnc.qzone.qq.com/cgi-bin/blognew/blog_output_toppage?uin='+QQnum+'&vuin=0&property=GoRE&getall=1&styledm=cnc.qzonestyle.gtimg.cn&imgdm=cnc.qzs.qq.com&bdm=b.cnc.qzone.qq.com&cate=&numperpage=100&maxlen=68&sorttype=0&pos=0&direct=1'
blogpreurl='http://user.qzone.qq.com/'+QQnum+'/blog/'

#返回页面
def visitUrl(url):  
    html=''
    fd=urllib2.urlopen(url)
    html=fd.read()
    if html !='':
        fd.close()
        return html
#写入文件
def write2file(data1, data2):
    fp=open('blogtitlelist.txt', 'a')
    for eachline in data1:
        fp.write(eachline)
        fp.write('\n')
    fp.close()
        
#下载网页
def downloadurl(url, filename):
    downloadFolder = './QQspacedown' #指定保存网页的文件夹
    if not os.path.isdir( downloadFolder ):
        os.mkdir( downloadFolder )
    downfilename=downloadFolder+'/'+filename+'.html'
    op=open(downfilename, 'wb')
    
    fd=urllib2.urlopen(url)
    html=fd.read()
    if html !='':
        op.write(html)
        fd.close()
        op.close()
        return True
    fd.close()
    op.close()
    return False
#处理html
class Parser(HTMLParser):   
    def __init__(self):   
        self.targets={}
        self.is_span=''
        HTMLParser.__init__(self)   
    def handle_starttag(self, tag, attrs):   
        if tag=='span':
            for name, value in attrs:
                if name=='id':
                    if value.startswith('blogtitle_'):
                        self.is_span=1
                        index=value.find('_')
                        self.id=value[index+1:]
    def handle_endtag(self, tag):
        if tag=='span':
            self.is_span=''
    def handle_data(self, data):
        if self.is_span:
            self.targets[self.id]=data.strip()
    def getTargets(self):   
        return self.targets
if __name__=='__main__':
    map={}
    titlelist=[]
    urllist=[]
    data=visitUrl(blogurl)
    myparser=Parser()
    myparser.feed(data)
    map=myparser.getTargets()
    for key, value in map.items():
        titlelist.append(value)
        urllist.append(blogpreurl+key)
    print urllist
    write2file(titlelist)
    print 'finished'

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞6
打赏
分享
最新回复 (6)
雪    币: 306
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
tfzxyinhao 2010-12-15 01:26
2
0
楼主能搞下qq魔法卡片的地址吗,flash太卡了,要是用程序直接操作就好了。
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
我是人 2010-12-15 02:12
3
0
看不到?看不到
雪    币: 1147
活跃值: (783)
能力值: ( LV13,RANK:260 )
在线值:
发帖
回帖
粉丝
ycmint 5 2010-12-15 08:33
4
0
标记。。。。喜欢代码。。
雪    币: 354
活跃值: (147)
能力值: ( LV7,RANK:100 )
在线值:
发帖
回帖
粉丝
haithink 1 2010-12-15 08:47
5
0
我也要学phthon!!!
雪    币: 239
活跃值: (25)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
dlmu 1 2010-12-15 09:07
6
0
好强大呀!能不能写成日志转换软件
雪    币: 111
活跃值: (35)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
flyliying 2010-12-15 09:50
7
0
直接遍历
'http://user.qzone.qq.com/'+QQnum+'/blog/'+i

i 从0到1000
游客
登录 | 注册 方可回帖
返回