日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

下载天涯的文章

發布時間:2024/6/14 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 下载天涯的文章 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫了個下載天涯文章的Python腳本,有點亂,效率很低,單線程,用正則表達式拼起來的。

能簡單的判斷一些是不是作者與網友聊天的帖子,也會有失誤,而且很多!!!!

有時候會卡死,我的解決辦法是重新來一次……汗

# -*- coding: utf-8 -*- import urllib import urllib2 import re,osdef cn(s):return s.decode("utf-8").encode("gbk")def getUrlContent(url):return urllib2.urlopen(url).read()def getFirst(cont):p1 = re.findall('''<div class="bbs-content clearfix">(.+?)</div>''',cont,re.DOTALL)if len(p1)>0:return p1[0]else:return ""def getNextPageUrl(cont):p1 = re.findall('''<a href="(.+?)" class="js-keyboard-next">下頁</a>''',cont)if len(p1)>0:return "http://bbs.tianya.cn"+p1[0]else:return Nonedef getAuthor(cont):p1 = re.findall('''<meta name="author" content="(.+)">''',cont)if len(p1)>0:return p1[0]def getTitle(cont): p1 = re.findall('''<span class="s_title"><span style="font-weight:400;">(.+?)</span>''',cont)if len(p1)>0:return p1[0]def getOnePage(cont,author,fp):t=""n=0#print contp='''<div class="atl-item".+?uname="(.+?)">.+?<span>(時間.+?)</span>.+?<div class="bbs-content">(.+?)</div>'''p1 = re.findall(p,cont,re.S)#print p1if len(p1)>0:for t in p1:if t[0]==author:if re.findall("[^-]+?-----------------------------[^-]*?",t[2])==[] and len(t[2])>512:fp.writelines("<hr/>%s<br/>%s"%(t[1],t[2]))def main(url):n=0author=""print urlcont=getUrlContent(url)if cont<0:returnprint 'open OK'author=getAuthor(cont)if author<0:print "url error"returntitle = getTitle(cont)if author<0:print "url error"returntime=re.findall("<span>(時間:.+?)</span>",cont)[0]print 'title:',cn(title)print 'author:',cn(author)print 'time:',cn(time)while 1:if n>0:fn="%s[%d].htm"%(cn(title),n)else:fn="%s.htm"%cn(title)if os.path.isfile(fn):print "File %s already exists!"%fnn=n+1else:breakfp=open(fn,'w')fp.writelines('''<html><head><meta charset="utf-8"/><title>%s</title></head><body>'''%(title))fp.writelines("【%s】<br/>【%s】\n<hr/>%s<br/>"%(title,author,time))fp.writelines(getFirst(cont))n=1while 1:print "page:%d"%ngetOnePage(cont,author,fp)url=getNextPageUrl(cont)if url!=None:cont=getUrlContent(url)n=n+1else:breakfp.writelines('''</body></html>''')fp.close()print "download ok"if __name__ == '__main__':url=raw_input('input url:')main(url)

  

轉載于:https://www.cnblogs.com/fwindpeak/p/3369383.html

總結

以上是生活随笔為你收集整理的下载天涯的文章的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。