Python

For cambridge

fork
loading
Files
  • main.py
  • definations.txt
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import urllib.request
from bs4 import BeautifulSoup

def main():
  wordw = input("seperate words with commas: ")
  words = wordw.split(",")
  for word in words:
    webUrl  = urllib.request.urlopen('https://dictionary.cambridge.org/dictionary/english-chinese-simplified/'+word)
    data = webUrl.read()
    soup = BeautifulSoup(data)
    myspans = soup.find("span", {"class": "hw"})
    
    mydef = soup.find_all("b", {"class": "def"})
    defi = ""
    for i in mydef:
      defi = defi + i.text + "\n\n"
    print(str(myspans.contents[0]) + ": " + defi + "\n")
    f = open("definations.txt", "a")
    f.write(str(myspans.contents[0]) + ": " + defi + "\n")
    f.close()
main()