通过beautifulsoup对json爬取的文件进行元素审查,获取是否含有p标签
# -*- coding:utf-8 -*- from lxml import html import requests import json import re import scrapy from bs4 import BeautifulSoup #通过beautifulsoup解析文档 def bs4analysis(html_doc): soup = BeautifulSoup(html_doc,"lxml") if soup.find_all(‘a‘): print soup.a.string print soup.a.nextSibling elif html_doc.find(‘#‘)>=0: print ‘有主题‘ p=re.split(‘#‘,html_doc) print ‘p0‘+p[0] print ‘p1‘+p[1] print ‘p2‘+p[2] else: print ‘haha‘ html_doc=‘‘‘ <a class=‘k‘ href=‘https://m.weibo.cn/k/SHU毕业季?from=feed‘>#毕业季#</a> 云端祝福!祝全体2017届毕业生及全球人前程似锦。 <a data-url="http://t.cn/RootR20" href="https://m.weibo.cn/p/index?containerid=230444def4f80e7a017ab35b3e37cadc001f32&url_type=39&object_type=video&pos=1&luicode=10000011&lfid=1076033243026514&featurecode=20000320&ep=F9u8aqkyn%2C3243026514%2CF9u8aqkyn%2C3243026514" data-hide=""><span class="url-icon"><img src="https://h5.sinaimg.cn/upload/2015/09/25/3/timeline_card_small_video_default.png"></span></i><span class="surl-text">秒拍视频</a> ‘‘‘ html_doc2=‘‘‘ #早安# 万木沉酣新雨后,百昌苏醒晓风前,四时可爱唯春色,一事能狂便少年。——王国维 ‘‘‘ html_doc3=‘‘‘ <a class=‘k‘ href=‘https://m.weibo.cn/k/通知公告?from=feed‘>#通知公告#</a>南区浴室男生区因突发水管爆裂,今日起将关闭2间浴室,请各位提早安排应对。 ‘‘‘ html_doc4=‘‘‘ 我发表了头条文章:《共建美术学院签约暨上海吴淞国际艺术城发展研究院揭牌仪式举行》 <a data-url=""http://t.cn/RK2rQFs"" href=""http://media.weibo.cn/article?object_id=1022%3A2309404126988389488631&url_type=39&object_type=article&pos=1&luicode=10000011&lfid=1076033243026514&id=2309404126988389488631&ep=Fbk5FbYMp%2C3243026514%2CFbk5FbYMp%2C3243026514"" data-hide=""""><span class=""url-icon""><img src=""https://h5.sinaimg.cn/upload/2015/09/25/3/timeline_card_small_article_default.png""></span></i><span class=""surl-text"">共建美术学院签约暨上海吴淞国际艺术城发展研究院揭牌仪式举行</a> ??? ‘‘‘ html_doc5=‘‘‘ <a class=‘k‘ href=‘https://m.weibo.cn/k/SHU分享?from=feed‘>#分享#</a> 斗转星移,岁月如梭<span class=""url-icon""><img src=""//h5.sinaimg.cn/m/emoticon/icon/others/l_xin-8e9a1a0346.png"" style=""width:1em;height:1em;"" [心]""></span> ‘‘‘ if __name__ == ‘__main__‘: f = open(‘shuweibo.txt‘, ‘r‘) fh = open(‘analysis.txt‘, ‘a‘) while True: line = f.readline() if line == ‘‘: break print ‘*******************‘ bs4analysis(line) print ‘*******************‘ f.close() fh.close()
时间: 2024-10-07 18:54:06