Python爬学校论坛民主湖(2)---------实现多个版块搜索

来源:互联网 发布:tensorflow mnist cnn 编辑:程序博客网 时间:2024/04/29 23:27
</pre><p>前两天实习了一个版块的搜索,但是多要想全面的了解某个人全面信息,肯定是不够的。一开始打算用正则来正则出所有版块的FID值,后来发现没必要这么麻烦,本来版块就不多,而且有些板块也没多大意思,比如公告版块,所以我就直接将FID保存成一个字典结构,如果你对某个版块感兴趣,还可以添加该版块的FID值。另外说一下本程序改变了停止策略,由于某个版块日发表量很少,有些却很多,如果同等页数搜索,就会造成搜索不平衡。我采用时间停止,设置一个时间,该时间以前的将不在搜索,你可以把这个时间设置成你要查找人的注册日期,他注册前,肯定没有发过帖子吧。<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />那么,我们上代码吧。</p><p><pre class="python" name="code"># -*- coding: utf-8 -*-"""Created on Fri Mar 06 18:13:48 2015可以爬到所有无需登录的模块本模块可以从第一页访问,然后根据用户注册时间与网页贴子时间相比较,适时结束访问,加快搜索速度。!输入参数:        用户ID        用户注册时间:需要登录查看@author: KyleHuang@Address: Chongqing University"""import re  import urllib2import datetimeimport timedef findAuthorArticleWithoutLogin(authorId,date1):    FidList={280:u'计算机技术',119:u'学术民主湖',14:u'江风竹雨',27:u'人文社科',63:u'好摄之徒',17:u'书香重大',              109:u'外语角',83:u'黄桷树下',123:u'鱼食天下',107:u'鱼游天下',30:u'激情天下',181:u'数码广场',             18:u'轻松一刻',92:u'老乡会所',195:u'健康大家谈',100:u'心语新缘',234:u'曝光台',103:u'张贴栏',             138:u'生物学院'             #203:u'租房',218:u'兼职',180:u'民主湖超市'             }    #findAuthorArticle(authorId,100,FidList[100],date1)    for key in FidList:       print FidList[key]       findAuthorArticle(authorId,key,FidList[key],date1)def findAuthorArticle(authorId,Fid,FidName,date1):    ###该函数主要用来访问近几年用户,从第一页访问     pageStart=1#从第一页访问     pageEnd=500#最多访问500页,结束     urlstr='http://www.cqumzh.cn/bbs/forumdisplay.php?fid='+str(Fid);     matchstr='space.php?uid='+str(authorId)     for i in range(pageStart,pageEnd):         print u'爬虫爬到第'+str(i)+u'页'         #合成URL路径         urlstr2=urlstr+'&page='+str(i)         #模拟请求网址         request = urllib2.Request(urlstr2)         request.add_header('User-Agent', 'fake-client')         response = urllib2.urlopen(request)         myPage =response.read()         #匹配目标内容         myItems=re.findall('<a title=(.*?)>(.*?)</a></span>.*?<td align="center" style="overflow:hidden"nowrap="nowrap">\r\n<cite>\r\n<a href="(.*?)">(.*?)</a>',myPage,re.S)         for item in myItems:              #print item[1]+'authour='+item[2]          #f.writelines(item[1]+'authour='+item[2]+'\n\r')             str1=str(item[2])             #找到目标作者             if str1 == matchstr :                 #addr=getWebAdress(item[0])                 info=item[0].replace('&','&')                 print FidName+u','+item[1]+','+info                 #保存到文件                 f.writelines(str(FidName)+u',第'+str(i)+u'页,'+item[1]+','+info+'\n\r')         length=len(myItems)         date2=getItemPage(myItems[length-1][0])         #print date2         dderror=date2-date1         if dderror.days<0:             returndef getWebAdress(objStr):             addr=re.findall('.*?href="(.*?)"',objStr,re.S)             return addr[0]def getItemPage(objStr):    mItems=re.findall('\d{4}-\d{1,2}-\d{1,2}',objStr,re.S)    mdate=datetime.datetime.strptime(str(mItems[0]), "%Y-%m-%d")    return mdateif __name__=="__main__":    #此处输入收缩需要的信息    authorId=133788        dtstr = '2011-9-8'    dd=datetime.datetime.strptime(dtstr, "%Y-%m-%d")    print u"爬虫开始爬民主湖了 ......"    f = open('Bid'+str(authorId)+'.txt','w+')      f.writelines(u"作者"+str(authorId)+u"\n\r")     findAuthorArticleWithoutLogin(authorId,dd)    f.close()

传了好几次图片,这次终于成功了!

0 0