我正在尝试修改 ANKI 插件 .py 文件,以便能够解析牛津的单词from urllib.parse import urlencodeqs = urlencode({"q": "come along"})URL = "https://www.oxfordlearnersdictionaries.com/search/english/?{}"print(URL.format(qs))我的输出“https://www.oxfordlearnersdictionaries.com/search/english/?q=come+along”但实际上输出应该是这样的“https://www.oxfordlearnersdictionaries.com/definition/english/come-along?q=come+along”我怎样才能克服这个问题我是初学者#-- coding:utf-8 --import randomfrom ..base import *import requestsfrom bs4 import BeautifulSoupfrom time import sleepfrom random import randint@register(u'Oxford_Article')class Oxford_Article(WebService): def init(self): super(Oxford_Article, self).init() def _get_from_api(self): sleep(randint(1,3)) param = {"q": "come along"} qs = urlencode(param) param['q'] = param['q'].replace(' ', '-') URL = "https://www.oxfordlearnersdictionaries.com/definition/english/{}?{}" data = self.get_response(URL.format(param['q'],qs)) soup = parse_html(data) result = { 'Article': u'', } # Article element = soup.find('div', id='entryContent') for s in element.select('script'): s.extract() if element: result['Article'] = u''.join(str(e) for e in element.contents) return self.cache_this(result) @export([u'entryContent', u'Article definition']) def fld_definate(self): return self._get_field('Article')
2 回答
当年话下
TA贡献1890条经验 获得超9个赞
你可以这样做
from urllib.parse import urlencode
param = {"q": "come along"}
qs = urlencode(param)
param['q'] = param['q'].replace(' ', '-')
URL = "https://www.oxfordlearnersdictionaries.com/definition/english/{}?{}"
print(URL.format(param['q'],qs))
胡说叔叔
TA贡献1804条经验 获得超8个赞
如果您在解析数据时遇到错误
只需替换这段代码
data = self.get_response(URL.format(param['q'],qs)) soup = parse_html(data)
用这个代码
page = urllib.request.urlopen(URL.format(param['q'],qs)) soup = BeautifulSoup(page)
也不要忘记导入这个
import urllib.request
添加回答
举报
0/150
提交
取消