1

I am trying to modify an ANKI addon .py file that to be able to parse words from oxford

from urllib.parse import urlencode
qs = urlencode({"q": "come along"})

URL = "https://www.oxfordlearnersdictionaries.com/search/english/?{}"

print(URL.format(qs))

my output

"https://www.oxfordlearnersdictionaries.com/search/english/?q=come+along"

but actually output should be like this

"https://www.oxfordlearnersdictionaries.com/definition/english/come-along?q=come+along"

how can I overcome this issue I am rather beginner

#-- coding:utf-8 --
import random
from ..base import *

import requests
from bs4 import BeautifulSoup
from time import sleep
from 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

2 Answers 2

2

you can do this

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))
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much it is working but it is not working on this I updated my thread
bro, I am not python developer I all I am doing, I open my notepad++ and modify related file I am doing that to able to modify my ANKI programme addon
2

if you're getting error while parsing the data

just replace this code

data = self.get_response(URL.format(param['q'],qs))
soup = parse_html(data)

with this code

page = urllib.request.urlopen(URL.format(param['q'],qs))
soup = BeautifulSoup(page)

also don't forget to import this

import urllib.request

1 Comment

I don't use any IDE to doing that just modifying file using with notepad++ thank for your effort. Appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.