This commit is contained in:
St.Huang 2018-08-05 21:06:14 +08:00
parent 801fa19be9
commit 0c99c212bb
2 changed files with 42 additions and 2 deletions

View File

@ -314,8 +314,7 @@ class WebService(Service):
def get_response(self, url, data=None, headers=None, timeout=10):
default_headers = {
'User-Agent': 'Mozilla/5.0',
'Accept-Encoding': 'gzip'
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
}
if headers:
default_headers.update(headers)

View File

@ -0,0 +1,41 @@
#-*- coding:utf-8 -*-
import os
import json
from ..base import *
@register([u'必应图片', u'Bing-Images'])
class Bing_Images(WebService):
bing_download_img = True
def __init__(self):
super(Bing_Images, self).__init__()
def _get_from_api(self):
url = u"http://cn.bing.com/images/search?q={}".format(self.quote_word)
html = self.get_response(url, timeout=10)
soup = parse_html(html)
result = {
'img': '',
}
#图片连接
tag = soup.find('a', class_='iusc')
if tag:
try:
data = json.loads(tag.get('m'))
result['img'] = data.get('turl', u'')
except:
pass
return self.cache_this(result)
@export([u'图片', u'Image'])
def fld_pinyin(self):
url = self._get_field('img')
if url and self.bing_download_img:
filename = get_hex_name(self.unique.lower(), url, 'jpg')
if os.path.exists(filename) or self.download(url, filename):
return self.get_anki_label(filename, 'img')
return ''