最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python抓取網(wǎng)頁(yè)內(nèi)容并進(jìn)行語(yǔ)音播報(bào)的方法

 更新時(shí)間:2018年12月24日 10:35:13   作者:jupeizhong  
今天小編就為大家分享一篇python抓取網(wǎng)頁(yè)內(nèi)容并進(jìn)行語(yǔ)音播報(bào)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

實(shí)測(cè)win7和raspbian均可,且raspbian可以直接調(diào)用omxplayer命令進(jìn)行播放。

利用百度的語(yǔ)音合成api進(jìn)行語(yǔ)音播報(bào),抓取的頁(yè)面是北大未名BBS的十大。

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

# -*- coding: utf-8 -*-
import urllib2
import 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運(yùn)行,抓取結(jié)果如下:

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

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

下面放語(yǔ)音播報(bào)模塊,也是整個(gè)程序的入口:

# -*- coding: utf-8 -*-
'''
Author  : Peizhong Ju
Latest Update : 2016/4/21
Function : Use Baidu Voice API to speak
'''
import urllib, urllib2
import json
import ConfigParser
import 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 it
except Exception as e:
 print "ERROR!"
 print e
 

當(dāng)中我們用到了config文件,便于記錄和修改,格式如下:

[Baidu]
client_id = HWWuh7dee6EBSAvzrOGaGNvX
client_secret = G3PwLHC5aCN2TQn3GcYjhn3BmH6xgxtR
token = 24.533d59e6554d133ea6bf02125bc6fa30.2592000.1463760851.282335-5802050
 
[Dir]
mp3 = C:\Users\jupeizhong\Desktop\python2\baiduVoice\hello.mp3

其中token是由程序生成的。

以上這篇python抓取網(wǎng)頁(yè)內(nèi)容并進(jìn)行語(yǔ)音播報(bào)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

二手房| 綦江县| 儋州市| 曲沃县| 隆安县| 九寨沟县| 裕民县| 项城市| 平潭县| 台州市| 凤冈县| 延寿县| 南京市| 河东区| 莱芜市| 古浪县| 习水县| 茶陵县| 虹口区| 万荣县| 平乐县| 墨脱县| 车险| 武平县| 汉川市| 松溪县| 龙山县| 新沂市| 迭部县| 曲沃县| 唐河县| 灵川县| 盐边县| 临洮县| 襄樊市| 民乐县| 鄂尔多斯市| 田林县| 泗洪县| 琼中| 新化县|