国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python抓取網頁內容并進行語音播報的方法

2020-02-16 00:17:26
字體:
來源:轉載
供稿:網友

python2.7,下面是跑在window上的,稍作修改就可以跑在linux上。

實測win7和raspbian均可,且raspbian可以直接調用omxplayer命令進行播放。

利用百度的語音合成api進行語音播報,抓取的頁面是北大未名BBS的十大。

先放抓取模塊BDWM.py的代碼:

# -*- coding: utf-8 -*-import urllib2import HTMLParser class MyParser(HTMLParser.HTMLParser): def __init__(self): HTMLParser.HTMLParser.__init__(self)  self.nowtag = '' self.count = 0 self.flag = False self.isLink = False self.count2 = 0 self.dict = {} self.temp = '' def handle_starttag(self, tag, attrs): if tag == 'span':  for key, value in attrs:  if key == 'class' and ('Rank1AmongHisBoard' in value):   self.count += 1   if self.count < 11:   self.flag = True if tag == 'a':  self.isLink = True else:  self.isLink = False def handle_data(self, data): if self.flag and self.isLink:  self.count2 += 1  if self.count2 == 1:  self.temp = data  if self.count2 == 3:  self.flag = False  self.count2 = 0  self.dict[self.temp] = data  res = urllib2.urlopen('https://www.bdwm.net/bbs/main0.php')my = MyParser()my.feed(res.read().decode("gbk"))result = ''str = " 版 "str = str.decode('utf8')for i in my.dict: result += i + str + my.dict[i] + '/n'print result

F5運行,抓取結果如下:

>>> ======================= RESTART =======================
>>>
化學與分子工程學院 版 不喜歡做實驗怎么辦
三角地 版 烈士旅正在對對研究生會實施最高軍事占領的
十六周年站慶 版 ★★畢業季 | 未名BBS歷年紀念品特賣會★★
遺跡保衛 版 母校兩日游,想借個飯卡
別問我是誰 版 遇到性騷擾,打電話跟男朋友傾訴……
美食天地 版 請問北大附近哪里有好吃的餃子
男孩子 版 被戴綠帽,萬念俱灰!
鵲橋 版 醫生mm征GG(#征男友#代征)
談情說愛 版 # 感覺身邊都是嘴上急著脫光但心里不急的人 #
北京大學研究生會 版 農園一層和自稱“常代會”的占座女吵起來了(轉載)(轉載)

可以看到我們成功抓取到了未名BBS十大的版面信息與標題。

下面放語音播報模塊,也是整個程序的入口:

# -*- coding: utf-8 -*-'''Author  : Peizhong JuLatest Update : 2016/4/21Function : Use Baidu Voice API to speak'''import urllib, urllib2import jsonimport ConfigParserimport BDWM config = ConfigParser.ConfigParser()config.readfp(open('config.ini'))TOKEN = config.get('Baidu', 'token')local = config.get('Dir', 'mp3')words = '' def GetVoice(): text = urllib.quote(words) url = 'http://tsn.baidu.com/text2audio?tex=' + text + '&cuid=b888e32e868c&lan=zh&ctp=1&tok=' + TOKEN rep = urllib.urlretrieve(url, local) CheckError() def GetAccessToken(): client_id = config.get('Baidu', 'client_id') client_secret = config.get('Baidu', 'client_secret') rep = urllib2.urlopen('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='+client_id+'&client_secret='+client_secret) hjson = json.loads(rep.read()) return hjson['access_token'] def CheckError(): global TOKEN file_object = open(local) try:  all_the_text = file_object.read()  if (all_the_text[0] == '{'):  hjson = json.loads(all_the_text)  #print hjson['err_no']  if (hjson['err_no'] == 502):   print 'Getting new access token...'   TOKEN = GetAccessToken()   config.set('Baidu', 'token', TOKEN)   config.write(open('config.ini', "r+"))   GetVoice()  else:   print all_the_text  else:  print '[success] ' + words finally:  file_object.close() try: words = BDWM.result.encode('utf8') GetVoice() # use other software to play itexcept Exception as e: print "ERROR!" print e             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彩票| 邯郸市| 嵊泗县| 南投县| 永州市| 仙居县| 龙江县| 盐山县| 商城县| 乌鲁木齐市| 措勤县| 榆中县| 东兰县| 密山市| 武鸣县| 秀山| 武定县| 武穴市| 广河县| 信阳市| 罗定市| 昌乐县| 潼南县| 华池县| 连南| 宣汉县| 黔南| 阳高县| 达州市| 平陆县| 肇州县| 华坪县| 乳山市| 延安市| 诏安县| 吉安市| 绍兴县| 务川| 连城县| 伊宁市| 六枝特区|