<reponame>hooooolyshit/VocabularyNoteBook<filename>bing.py
import requests
import bs4
import time
import random

# 暂时没用的class


class ExampleSentance:
    meanings = ''
    sentences = []

    def set_meaning(self, meanings):
        self.meanings = meanings

    def add_sentence(self, s):
        if type(s) == str:
            self.sentences.append(s)
        elif type(s) == list:
            for each in s:
                self.add_sentence(each)
        else:
            self.sentences.append('ERROR')


class Meaning:
    type_word = ''
    paraphrase = ''

    def __init__(self, thetype, trans) -> None:
        self.type_word = thetype
        self.paraphrase = trans


class Bing:

    def __init__(self, word):
        self.example_sentances = []
        self.tense = ''
        self.meanings = []
        self.word = word
        self.url = "https://cn.bing.com/dict/search?q="+word

    def get_html(self):
        headers = {
            'authority': 'cn.bing.com',
            'sec-ch-ua': '^\\^',
            'sec-ch-ua-mobile': '?0',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
            'sec-ch-ua-arch': '^\\^x86^\\^',
            'x-autosuggest-contentwidth': '648',
            'sec-ch-ua-full-version': '^\\^90.0.4430.212^\\^',
            'sec-ch-ua-platform-version': '^\\^10.0^\\^',
            'sec-ch-ua-model': '',
            'sec-ch-ua-platform': '^\\^Windows^\\^',
            'accept': '*/*',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'accept-language': 'zh-CN,zh;q=0.9',
            'Referer': 'https://cn.bing.com/',
        }
        html = requests.get(self.url, headers=headers)
        return html

    def get_meanings(self, soup):
        meanings = soup.find_all('span', class_='def b_regtxt')
        print("正在寻找", self.word, "的释义")
        for each in meanings:
            a = Meaning(each.text, each.previous)
            self.meanings.append(a)

    def get_tense(self, soup):
        print("正在寻找", self.word, "的时态:")
        tense = soup.find(class_="hd_div1")
        if(tense):
            self.tense = tense.text

    def get_sentences(self, soup):
        sentences = soup.find_all('div', class_='sen_en b_regtxt')
        print("爬取", self.word, "的例句中༼ つ ◕_◕ ༽つ")
        for each in sentences:
            self.example_sentances.append(each.text)

    def save(self):
        file_name = 'VocabularyNotebook'
        try:
            f = open(file_name + '.txt', 'r', encoding='utf-8')
        except FileNotFoundError:
            f = open(file_name + '.txt', 'w+', encoding='utf-8')
        f.close()
        with open(file_name + '.txt', 'a', encoding='utf-8') as file:
            file.write(self.word + '\n')
            if self.tense != '':
                file.write(self.tense+'\n')
            file.write('释义:\n')
            count = 0
            for each in self.meanings:
                file.write('\t'+each.paraphrase+" "+each.type_word+"\n")
                count += 1
                if count >= 3:
                    count = 0
                    break

            file.write('例句：\n')
            for each in self.example_sentances:
                file.write('\t'+each+"\n")
                count += 1
                if count >= 4:
                    count = 0
                    break
            file.write('\n\n')
        print(self.word+"已经写入文件并保存")

    def run(self):
        html = self.get_html()
        soup = bs4.BeautifulSoup(html.text, 'lxml')
        # 时态 第三人称单数 过去分词、、、
        self.get_tense(soup)
        # 各种词性的释义
        self.get_meanings(soup)
        # 例句
        self.get_sentences(soup)
        self.save()


if __name__ == "__main__":
    # 前期准备工作
    word_list = []
    f = open('VocabularyList.txt', 'r', encoding='utf-8')
    str = f.read()
    f.close()
    word_list = str.split(" ")
    random.shuffle(word_list)
    max_num_of_words = len(word_list)
    print("想写多少个单词?\n最多", max_num_of_words, '个: ', end="")
    times = int(input()) % len(word_list)
    timeGap = float(input("爬取间隔(推荐5s以上): "))
    # 去 单词本中瞧瞧吧
    for each in word_list:
        if(times < 0):
            break
        myBing = Bing(each)
        print("正在尝试爬取: "+each)
        myBing.run()
        time.sleep(timeGap)
        times -= 1
    input("程序运行结束, 打开\"VocabularyNotebook\"看看吧")

import requests
import bs4
import time
import random

# 暂时没用的class


class ExampleSentance:
    meanings = ''
    sentences = []

    def set_meaning(self, meanings):
        self.meanings = meanings

    def add_sentence(self, s):
        if type(s) == str:
            self.sentences.append(s)
        elif type(s) == list:
            for each in s:
                self.add_sentence(each)
        else:
            self.sentences.append('ERROR')


class Meaning:
    type_word = ''
    paraphrase = ''

    def __init__(self, thetype, trans) -> None:
        self.type_word = thetype
        self.paraphrase = trans


class Bing:

    def __init__(self, word):
        self.example_sentances = []
        self.tense = ''
        self.meanings = []
        self.word = word
        self.url = "https://cn.bing.com/dict/search?q="+word

    def get_html(self):
        headers = {
            'authority': 'cn.bing.com',
            'sec-ch-ua': '^\\^',
            'sec-ch-ua-mobile': '?0',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
            'sec-ch-ua-arch': '^\\^x86^\\^',
            'x-autosuggest-contentwidth': '648',
            'sec-ch-ua-full-version': '^\\^90.0.4430.212^\\^',
            'sec-ch-ua-platform-version': '^\\^10.0^\\^',
            'sec-ch-ua-model': '',
            'sec-ch-ua-platform': '^\\^Windows^\\^',
            'accept': '*/*',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'accept-language': 'zh-CN,zh;q=0.9',
            'Referer': 'https://cn.bing.com/',
        }
        html = requests.get(self.url, headers=headers)
        return html

    def get_meanings(self, soup):
        meanings = soup.find_all('span', class_='def b_regtxt')
        print("正在寻找", self.word, "的释义")
        for each in meanings:
            a = Meaning(each.text, each.previous)
            self.meanings.append(a)

    def get_tense(self, soup):
        print("正在寻找", self.word, "的时态:")
        tense = soup.find(class_="hd_div1")
        if(tense):
            self.tense = tense.text

    def get_sentences(self, soup):
        sentences = soup.find_all('div', class_='sen_en b_regtxt')
        print("爬取", self.word, "的例句中༼ つ ◕_◕ ༽つ")
        for each in sentences:
            self.example_sentances.append(each.text)

    def save(self):
        file_name = 'VocabularyNotebook'
        try:
            f = open(file_name + '.txt', 'r', encoding='utf-8')
        except FileNotFoundError:
            f = open(file_name + '.txt', 'w+', encoding='utf-8')
        f.close()
        with open(file_name + '.txt', 'a', encoding='utf-8') as file:
            file.write(self.word + '\n')
            if self.tense != '':
                file.write(self.tense+'\n')
            file.write('释义:\n')
            count = 0
            for each in self.meanings:
                file.write('\t'+each.paraphrase+" "+each.type_word+"\n")
                count += 1
                if count >= 3:
                    count = 0
                    break

            file.write('例句：\n')
            for each in self.example_sentances:
                file.write('\t'+each+"\n")
                count += 1
                if count >= 4:
                    count = 0
                    break
            file.write('\n\n')
        print(self.word+"已经写入文件并保存")

    def run(self):
        html = self.get_html()
        soup = bs4.BeautifulSoup(html.text, 'lxml')
        # 时态 第三人称单数 过去分词、、、
        self.get_tense(soup)
        # 各种词性的释义
        self.get_meanings(soup)
        # 例句
        self.get_sentences(soup)
        self.save()


if __name__ == "__main__":
    # 前期准备工作
    word_list = []
    f = open('VocabularyList.txt', 'r', encoding='utf-8')
    str = f.read()
    f.close()
    word_list = str.split(" ")
    random.shuffle(word_list)
    max_num_of_words = len(word_list)
    print("想写多少个单词?\n最多", max_num_of_words, '个: ', end="")
    times = int(input()) % len(word_list)
    timeGap = float(input("爬取间隔(推荐5s以上): "))
    # 去 单词本中瞧瞧吧
    for each in word_list:
        if(times < 0):
            break
        myBing = Bing(each)
        print("正在尝试爬取: "+each)
        myBing.run()
        time.sleep(timeGap)
        times -= 1
    input("程序运行结束, 打开\"VocabularyNotebook\"看看吧")
# 暂时没用的class # 时态 第三人称单数 过去分词、、、 # 各种词性的释义 # 例句 # 前期准备工作 # 去 单词本中瞧瞧吧