最近看天涯上的连续小说, 感到有些地方很不方便,比如: 作者写了一篇后, 后面很多人回复, 然后作者在写, 别人再回复, 导致看一篇完整的文章时, 需要自己可能得翻很多次页才能看完. 于是编写了这个python脚本, 只要用户输入帖子首页的网址, 和作者名(如果不输入,就认为是第一篇文章的作者), 就能将该帖子中该作者写的内容全部提取出来


python:下载天涯论坛上指定作者的文章# -*- coding: mbcs -*-
python:下载天涯论坛上指定作者的文章#
import re,urllib, urllib2, cookielib,datetime,sys
python:下载天涯论坛上指定作者的文章
import urllib2,sys,os
python:下载天涯论坛上指定作者的文章
python:下载天涯论坛上指定作者的文章
def    getUrlContent(url):
python:下载天涯论坛上指定作者的文章    ifile 
= urllib2.urlopen(url)
python:下载天涯论坛上指定作者的文章    data
=ifile.read()
python:下载天涯论坛上指定作者的文章    ifile.close()
python:下载天涯论坛上指定作者的文章    
return data
python:下载天涯论坛上指定作者的文章
python:下载天涯论坛上指定作者的文章
def getNextPageUrl(cont):
python:下载天涯论坛上指定作者的文章    p0
=0
python:下载天涯论坛上指定作者的文章    res
=[]
python:下载天涯论坛上指定作者的文章    
while 1:
python:下载天涯论坛上指定作者的文章        p1 
= cont.find("<table border=0>",p0)
python:下载天涯论坛上指定作者的文章        
if p1<0:break
python:下载天涯论坛上指定作者的文章        p2 
= cont.find('<font color=black>共',p1)
python:下载天涯论坛上指定作者的文章        
if p2<0:break
python:下载天涯论坛上指定作者的文章        p3 
= cont.find('><font color=#246cae>下一页',p2)
python:下载天涯论坛上指定作者的文章        
if p3<0:break
python:下载天涯论坛上指定作者的文章        tmpl
='<a  href='
python:下载天涯论坛上指定作者的文章        p4 
= cont.rfind(tmpl,p2,p3)
python:下载天涯论坛上指定作者的文章        
if p4<0:break
python:下载天涯论坛上指定作者的文章        p0
=p4
python:下载天涯论坛上指定作者的文章        res.append(cont[p4
+len(tmpl):p3])
python:下载天涯论坛上指定作者的文章        
return res[0]
python:下载天涯论坛上指定作者的文章    
return None
python:下载天涯论坛上指定作者的文章
python:下载天涯论坛上指定作者的文章
def    getAuthor(cont):    
python:下载天涯论坛上指定作者的文章    p1 
= cont.find("<TABLE")
python:下载天涯论坛上指定作者的文章    
if p1<0:return None
python:下载天涯论坛上指定作者的文章    p1
=cont.find('>作者:<a', p1)
python:下载天涯论坛上指定作者的文章    
if p1<0:return None
python:下载天涯论坛上指定作者的文章    p1
=cont.find("vwriter=", p1)
python:下载天涯论坛上指定作者的文章    
if p1<0:return None
python:下载天涯论坛上指定作者的文章    s1
="target=_blank>"
python:下载天涯论坛上指定作者的文章    p1
=cont.find(s1, p1)
python:下载天涯论坛上指定作者的文章    
if p1<0:return None
python:下载天涯论坛上指定作者的文章    s2
="</a>"
python:下载天涯论坛上指定作者的文章    p2
=cont.find(s2, p1)
python:下载天涯论坛上指定作者的文章    
if p2<0:return None
python:下载天涯论坛上指定作者的文章    
return cont[p1+len(s1):p2]
python:下载天涯论坛上指定作者的文章    
python:下载天涯论坛上指定作者的文章
def    getTitle(cont):    
python:下载天涯论坛上指定作者的文章    s1
='<TITLE>'
python:下载天涯论坛上指定作者的文章    p1
=cont.find(s1)
python:下载天涯论坛上指定作者的文章    
if p1<0:return None
python:下载天涯论坛上指定作者的文章    p2
=cont.find('</TITLE>',p1)
python:下载天涯论坛上指定作者的文章    
if p2<0:return None
python:下载天涯论坛上指定作者的文章    
return cont[p1+len(s1):p2]
python:下载天涯论坛上指定作者的文章    
python:下载天涯论坛上指定作者的文章
def    getByAuthor(cont,author):
python:下载天涯论坛上指定作者的文章    p0
=0
python:下载天涯论坛上指定作者的文章    res
=[]
python:下载天涯论坛上指定作者的文章    
while 1:
python:下载天涯论坛上指定作者的文章        p1 
= cont.find("<TABLE",p0)
python:下载天涯论坛上指定作者的文章        
#print 'p1',p1
python:下载天涯论坛上指定作者的文章
        if p1<0:break
python:下载天涯论坛上指定作者的文章        p2
=cont.find("vwriter=", p1)
python:下载天涯论坛上指定作者的文章        
if p2<0:break
python:下载天涯论坛上指定作者的文章        p2
=cont.find(">"+author+"</a>", p2)
python:下载天涯论坛上指定作者的文章        
#print 'p2',p2
python:下载天涯论坛上指定作者的文章
        if p2<0:break
python:下载天涯论坛上指定作者的文章        p3
=cont.find("</table>", p2)
python:下载天涯论坛上指定作者的文章        
#print 'p3',p3
python:下载天涯论坛上指定作者的文章
        if p3<0:break
python:下载天涯论坛上指定作者的文章        p4
=cont.find("<TABLE", p3)
python:下载天涯论坛上指定作者的文章        
#print 'p4',p4
python:下载天涯论坛上指定作者的文章
        if p4<0:
python:下载天涯论坛上指定作者的文章            p4
=cont.find("<!-- google_ad_section_end -->", p3)
python:下载天涯论坛上指定作者的文章            
assert(p4>0)
python:下载天涯论坛上指定作者的文章            res.append(cont[p3
+8:p4:])
python:下载天涯论坛上指定作者的文章            
break
python:下载天涯论坛上指定作者的文章        
else:
python:下载天涯论坛上指定作者的文章            p0
=p4
python:下载天涯论坛上指定作者的文章            res.append(cont[p3
+8:p4:])
python:下载天涯论坛上指定作者的文章        
python:下载天涯论坛上指定作者的文章    
return res
python:下载天涯论坛上指定作者的文章
python:下载天涯论坛上指定作者的文章
#url=sys.argv[1]
python:下载天涯论坛上指定作者的文章

python:下载天涯论坛上指定作者的文章
def mainProg(url):
python:下载天涯论坛上指定作者的文章    fp
=None
python:下载天涯论坛上指定作者的文章    author
=""
python:下载天涯论坛上指定作者的文章    
while 1:
python:下载天涯论坛上指定作者的文章        
print url
python:下载天涯论坛上指定作者的文章        cont
=getUrlContent(url)
python:下载天涯论坛上指定作者的文章        
print 'down OK'
python:下载天涯论坛上指定作者的文章        
if len(author)==0:
python:下载天涯论坛上指定作者的文章            author
=getAuthor(cont)
python:下载天涯论坛上指定作者的文章            title 
= getTitle(cont)
python:下载天涯论坛上指定作者的文章            
print 'author:',author,'title:',title
python:下载天涯论坛上指定作者的文章            title
=title.replace('/','x').replace('\\','x').replace(':','x').replace('*','x').replace('?','x')
python:下载天涯论坛上指定作者的文章            file
=title+'.htm'
python:下载天涯论坛上指定作者的文章            
if os.path.isfile(file):
python:下载天涯论坛上指定作者的文章                
print "File already exists!"
python:下载天涯论坛上指定作者的文章                
return
python:下载天涯论坛上指定作者的文章            fp
=open(file,'w')
python:下载天涯论坛上指定作者的文章        res
=getByAuthor(cont,author)
python:下载天涯论坛上指定作者的文章        
print 'parse ok',len(res)
python:下载天涯论坛上指定作者的文章        fp.writelines([url
+'<br>\n','<br>--------<br>'.join(res)])
python:下载天涯论坛上指定作者的文章        url
=getNextPageUrl(cont)
python:下载天涯论坛上指定作者的文章        
if url is None:
python:下载天涯论坛上指定作者的文章            
break
python:下载天涯论坛上指定作者的文章
python:下载天涯论坛上指定作者的文章
while 1:
python:下载天涯论坛上指定作者的文章    url
=raw_input('input url:')
python:下载天涯论坛上指定作者的文章    mainProg(url)

相关文章: