This commit is contained in:
St.Huang 2018-10-28 11:44:46 +08:00
parent bd6c0b3d2d
commit 13bf8f6c30
2 changed files with 98 additions and 81 deletions

View File

@ -22,10 +22,16 @@ class Cambridge(WebService):
'def': '' 'def': ''
} }
#english
element = soup.find('div', class_='link')
if element:
#页 #页
element = soup.find('div', class_='entry-body__el clrd js-share-holder') elements = element.find_all('div', class_='entry-body__el clrd js-share-holder')
header_found = False
for element in elements:
if element: if element:
#音 #音
if not header_found:
header = element.find('div', class_='pos-header') header = element.find('div', class_='pos-header')
if header: if header:
tags = header.find_all('span', class_='pron-info') tags = header.find_all('span', class_='pron-info')
@ -39,23 +45,25 @@ class Cambridge(WebService):
snd = tag.find('span', class_='circle circle-btn sound audio_play_button') snd = tag.find('span', class_='circle circle-btn sound audio_play_button')
if snd: if snd:
result['pronunciation'][pn+'mp3'] = cambridge_url_base + snd.get('data-src-mp3') result['pronunciation'][pn+'mp3'] = cambridge_url_base + snd.get('data-src-mp3')
header_found = True
#义 #义
body = element.find('div', class_='pos-body') body = element.find('div', class_='pos-body')
if body: if body:
tags = body.find_all('div', class_='def-block pad-indent') tags = body.find_all('div', class_='def-block pad-indent')
if tags: if tags:
l = [] l = result['def_list']
for tag in tags: for tag in tags:
i = tag.find('span', class_='def-info') i = tag.find('span', class_='def-info')
d = tag.find('b', class_='def') d = tag.find('b', class_='def')
e = tag.find('div', class_='examp emphasized') es = tag.find_all('div', class_='examp emphasized')
l.append( l.append(
u'<li><span class="epp-xref">{0}</span>\ u'<li>{0}{1}{2}</li>'.format(
<b class="def">{1}</b>\ u'<span class="epp-xref">{0}</span>'.format(str(i.get_text()).decode('utf-8')) if i else u'',
<div class="examp">{2}</div></li>'.format( u'<b class="def">{0}</b>'.format(str(d.get_text()).decode('utf-8')) if d else u'',
str(i.get_text()).decode('utf-8') if i else u'', u''.join(
str(d.get_text()).decode('utf-8') if d else u'', u'<div class="examp">{0}</div>'.format(str(e.get_text()).decode('utf-8')) if e else u''
str(e.get_text()).decode('utf-8') if e else u'' for e in es
)
) )
) )
result['def'] = u'<ul>' + u''.join(s for s in l) + u'</ul>' result['def'] = u'<ul>' + u''.join(s for s in l) + u'</ul>'

View File

@ -19,13 +19,20 @@ class Cambridge(WebService):
'pronunciation': {'AmE': '', 'BrE': '', 'AmEmp3': '', 'BrEmp3': ''}, 'pronunciation': {'AmE': '', 'BrE': '', 'AmEmp3': '', 'BrEmp3': ''},
'image': '', 'image': '',
'thumb': '', 'thumb': '',
'def': '' 'def': '',
'def_list': []
} }
#english
element = soup.find('div', class_='link')
if element:
#页 #页
element = soup.find('div', class_='entry-body__el clrd js-share-holder') elements = element.find_all('div', class_='entry-body__el clrd js-share-holder')
header_found = False
for element in elements:
if element: if element:
#音 #音
if not header_found:
header = element.find('div', class_='pos-header') header = element.find('div', class_='pos-header')
if header: if header:
tags = header.find_all('span', class_='pron-info') tags = header.find_all('span', class_='pron-info')
@ -39,23 +46,25 @@ class Cambridge(WebService):
snd = tag.find('span', class_='circle circle-btn sound audio_play_button') snd = tag.find('span', class_='circle circle-btn sound audio_play_button')
if snd: if snd:
result['pronunciation'][pn+'mp3'] = cambridge_url_base + snd.get('data-src-mp3') result['pronunciation'][pn+'mp3'] = cambridge_url_base + snd.get('data-src-mp3')
header_found = True
#义 #义
body = element.find('div', class_='pos-body') body = element.find('div', class_='pos-body')
if body: if body:
tags = body.find_all('div', class_='def-block pad-indent') tags = body.find_all('div', class_='def-block pad-indent')
if tags: if tags:
l = [] l = result['def_list']
for tag in tags: for tag in tags:
i = tag.find('span', class_='def-info') i = tag.find('span', class_='def-info')
d = tag.find('b', class_='def') d = tag.find('b', class_='def')
e = tag.find('div', class_='examp emphasized') es = tag.find_all('div', class_='examp emphasized')
l.append( l.append(
u'<li><span class="epp-xref">{0}</span>\ u'<li>{0}{1}{2}</li>'.format(
<b class="def">{1}</b>\ u'<span class="epp-xref">{0}</span>'.format(i.get_text()) if i else u'',
<div class="examp">{2}</div></li>'.format( u'<b class="def">{0}</b>'.format(d.get_text()) if d else u'',
i.get_text() if i else u'', u''.join(
d.get_text() if d else u'', u'<div class="examp">{0}</div>'.format(e.get_text()) if e else u''
e.get_text() if e else u'' for e in es
)
) )
) )
result['def'] = u'<ul>' + u''.join(s for s in l) + u'</ul>' result['def'] = u'<ul>' + u''.join(s for s in l) + u'</ul>'