我无法通过 beautifulsoup 获得我需要的信息,我的例句抓取器 BeautifulSoup

I can't get the information I need out with beautifulsoup for my example sentence grabber with BeautifulSoup

我一直在尝试使用 beautiful soup 制作一个简单的函数,以从一个名为 Tangorin 的网站上提取一本日语书籍的所有例句。我尝试编写两种不同类型的函数来提取这些数据,但我无法将其提取到 work.Sorry 这是一个很长的问题,但我尝试了一些不起作用的方法只编码了 3 周。我尝试提取的数据结构如下所示:

下面是网页上以英语作为搜索词的句子之一的数据结构,网页 https://tangorin.com/sentences?search=英語

我正在尝试将页面上的所有句子提取到单独句子的列表中。每个句子都在下面的块中,我已经突出显示了我要查找的关键信息。

**<div class="entry entry-border sentences undefined ">**
   <div class="entry-menu-wrap">
      <button class="entry-menu-btn btn">
         <svg class="icon" role="img" alt="" width="1em" height="1em" viewBox="0 0 50 50">
            <use xlink:href="#icon-chevron-down">
            </use>
         </svg>
      </button>
    </div>
  <dt class="s-jp" lang="ja">
      <a href="/words?search=話 %23n">
        <ruby>
           **話**
           <rt class="roma">hanashi</rt>
        </ruby>
      </a>
      <a href="/words?search=を %23part">
        <ruby>
           **を**
           <rt class="roma">wo</rt>
        </ruby>
      </a>
      <a href="/words?search=為る %23v">
        <ruby>
          **する**
          <rt class="roma">suru</rt>
        </ruby>
      </a>
      <a href="/words?search=事 %23n">
        <ruby>
           **こと**
           <rt class="roma">koto</rt>
        </ruby>
      </a>
      <a href="/words?search=で %23part">
        <ruby>
           **で**
           <rt class="roma">de</rt>
        </ruby>
      </a>
      <a href="/words?search=自分 %23n">
        <ruby>
           **自分**
           <rt class="roma">jibun</rt>
        </ruby>
      </a>
      <a href="/words?search=を %23part">
        <ruby>
           **を**
           <rt class="roma">wo</rt>
        </ruby>
      </a>
      <ruby>
         **曝け出す**
         <rt class="roma">曝kedasu</rt>
      </ruby>
      <a href="/words?search=事 %23n">
        <ruby>
           **こと**
           <rt class="roma">koto</rt>
        </ruby>
      </a>
      <a href="/words?search=を %23part">
        <ruby>
           **を**
           <rt class="roma">wo</rt>
        </ruby>
      </a>
      <a href="/words?search=恐れる %23v">
        <ruby>
           **恐れず**
           <rt class="roma">osorezu</rt>
        </ruby>
      </a>
      **、**
      <a href="/words?search=英語 %23n">
        <mark>
          <ruby>
             **英語**
             <rt class="roma">eigo</rt>
          </ruby>
        </mark>
      </a>
      <a href="/words?search=で %23part">
        <ruby>
           **で**
           <rt class="roma">de</rt>
        </ruby>
      </a>
      <a href="/words?search=他人 %23n">
        <ruby>
           **他人**
           <rt class="roma">tanin</rt>
        </ruby>
      </a>
      <a href="/words?search=と %23part">
        <ruby>
           **と**
           <rt class="roma">to</rt>
        </ruby>
      </a>
      <a href="/words?search=喋る %23v">
        <ruby>
           **しゃべる**
           <rt class="roma">shaberu</rt>
        </ruby>
      </a>
      <a href="/words?search=有らゆる %23pn-adj">
        <ruby>
           **あらゆる**
           <rt class="roma">arayuru</rt>
        </ruby>
      </a>
      <a href="/words?search=機会 %23n">
        <ruby>
           **機会**
           <rt class="roma">kikai</rt>
        </ruby>
      </a>
      <a href="/words?search=を %23part">
        <ruby>
           **を**
           <rt class="roma">wo</rt>
        </ruby>
      </a>
      <a href="/words?search=捕らえる %23v">
        <ruby>
          **とらえ**
          <rt class="roma">torae</rt>
        </ruby>
      </a>
      <a href="/words?search=なさる %23v">
        <ruby>
           **なさい**
           <rt class="roma">nasai</rt>
        </ruby>
      </a>
      **。**
      <a href="/words?search=そうすれば %23adv">
        <ruby>
           **そうすれば**
           <rt class="roma">sousureba</rt>
        </ruby>
      </a>
      <a href="/words?search=直に %23n">
        <ruby>
            **じきに**
            <rt class="roma">jikini</rt>
        </ruby>
      </a>
      <a href="/words?search=形式張る %23n">
        <ruby>
           **形式張らない**
           <rt class="roma">keishikiharanai</rt>
        </ruby>
      </a>
      <a href="/words?search=会話 %23n">
        <ruby>
           **会話**
           <rt class="roma">kaiwa</rt>
        </ruby>
      </a>
      <a href="/words?search=の %23part">
        <ruby>
            **の**
            <rt class="roma">no</rt>
        </ruby>
      </a>
      <a href="/words?search=場面 %23n">
        <ruby>
           **場面**
           <rt class="roma">bamen</rt>
        </ruby>
      </a>
      <a href="/words?search=で %23part">
        <ruby>
           **で**
           <rt class="roma">de</rt>
        </ruby>
      </a>
      <a href="/words?search=気楽 %23n">
        <ruby>
           **気楽**
           <rt class="roma">kiraku</rt>
        </ruby>
      </a>
      <a href="/words?search=に %23part">
        <ruby>
           **に**
           <rt class="roma">ni</rt>
        </ruby>
      </a>
      <a href="/words?search=慣れる %23v">
        <ruby>
           **なれる**
           <rt class="roma">nareru</rt>
        </ruby>
      </a>
      <a href="/words?search=である %23aux-v">
        <ruby>
           **であろう**
           <rt class="roma">dearou</rt>
        </ruby>
      </a>
      **。**
  </dt>

所以问题出在 class 我想要的数据以三种方式之一存储,在 a=href 超链接中,然后在 ruby 缩写中。或者在不在 a=href 内的 ruby 缩写中,或者有时在纯文本行中出现“、”、“。”、“?”等等

所以我在朋友使用 beautiful Soup 的帮助下编写了以下代码:

# all of your sentences from anki deck
# also new sentences will go here
ALL_SENTENCES = set()


# This piece of code returns true if char is in the set of all roman letters
# and false if not
def is_english(char):
    lower_case = ord("a") <= ord(char) <= ord("z")
    upper_case = ord("A") <= ord(char) <= ord("Z")
    return lower_case or upper_case


# This piece of code is to take a random Japanese word from a file and
# generate a Tangorin URL to the page of example
# sentencese for that word
def make_url(word):
    return f"https://tangorin.com/sentences?search={word}"


# This function searches through all of the descendants that have been added into the total
def filter_jap(sentences):
    jap_only = [
        [word for word in sentence if not is_english(word[0])] for sentence in sentences
    ]
    for sentence in jap_only:
        as_string = "".join(sentence) + "\n"
        print(as_string)


def get_random_sentence(all_sentences):
    return random.choice(all_sentences)


def get_example_sentences(word):
    url = make_url(word)
    source = requests.get(url).text
    soup = BeautifulSoup(source, "lxml")
    all_sentences = []
    curr_sentence = ""
    for sentence in soup.findAll(
        "div", class_="entry entry-border sentences undefined"
    ):
        character_blocks = sentence.dt
        for desc in character_blocks.descendants:
            # end of sentence detected, add curr sentence to all sentence list
            # and reset curr sentence
            if desc == "。":
                all_sentences.append(curr_sentence)
                curr_sentence = ""
            # if character is non-english (japanese) add it to current sentence
            elif type(desc) == NavigableString and not is_english(desc[0]):
                curr_sentence += desc
    return all_sentences

def gen_example_sentence(word):
    all_sentences = get_example_sentences(word)
    random_choice = get_random_sentence(all_sentences)
    return(random_choice)

with open('Japanese Words.txt', 'r', encoding="utf-8") as f:
    for line in f:
        x = gen_example_sentence(line)
        print(x)`

此代码的问题在于遇到这样的块时:

  <ruby>
         **曝け出す**
         <rt class="roma">曝kedasu</rt>
      </ruby>

此处 ruby 缩写的 rt class 中的项目格式不正确,开头为日文字符,因此 for 循环理解解析了以 an 开头的 navigablestring 类型的后代英文字符在这里完全失败。所以我尝试了一种不同的字符串操作方法,但我的编码技能仍然不够好,所以这是一个严重的失败:

`def make_url(word):
    return f"https://tangorin.com/sentences?search={word}"

word = "英語"
url = make_url(word)
source = requests.get(url).text
soup = BeautifulSoup(source, "lxml")
all_sentences = []
curr_sentence = ""
for sentence in soup.findAll("div", class_="entry entry-border sentences undefined"):
    y = sentence.dt
    for line in y:
        if str(type(line)) == "<class 'bs4.element.Tag'>":
            x = str(line.ruby)
            block = str(x.split('<'))
            block = str(block.split('>'))
            print(block)[3]
        #else:
            #curr_sentence += line
        #print(curr_sentence)
        #curr_sentence = ""`

我不知道如何解决这个问题来提取我需要的准确信息来成功地重新编译句子,然后将它们添加到一组字符串中。

对于一些额外的日语基础知识,日语句子的字符之间没有空格,除了在某些字符中内置空格,例如 '、' '。' '「' '」'

此外,ruby 标签是一种叫做 ruby 缩写的东西,它是一种描述日文字符位置的方式,以及您在其上方加上英文字符的阅读方式。 “roma”代表 romaji,意思是日语单词的英文字符读法

抱歉,这是一个很长的问题,但我在 beautifulsoup 和其他解析方法上看了很多 youtube 视频,但我就是想不通这个问题

此外,以防万一 bold 不起作用,在 xml 块内 ** ** 指标中的内容是我想要提取的位出去。本质上我想要的是 ruby 缩写里面的东西,而不是 rt class 里面的东西,我还想要那些纯文本项目“、”、“。”等等。如果你 运行原始后代代码你会得到 99% 准确的句子,看看我想要什么样的输出。预先感谢任何帮助过你的人!!!

可以先提取英文标签,再使用.get_text()

例如(希望输出正确,我看不懂日文):

import requests
from bs4 import BeautifulSoup


url = 'https://tangorin.com/sentences?search=%E8%8B%B1%E8%AA%9E'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')

for s in soup.select('.sentences'):
    soup.select_one('.s-en').extract()
    for r in s.select('.roma'):
        r.extract()
    print(s.get_text(strip=True))

打印:

私の母はあまり英語が上手に話せない。
2、3ページの英語を訳すのに2時間以上もかかりました。
こんなに上手に英語で手紙を書けるのにどうして話せないの?
英語のコミュニティでお名前とコメントを拝見し、プロフィールを拝読しました。
誰か英語を話す人はいますか。
「未来形」というのは存在しない、ということは受験英語でも一般的になりつつあります。
「順路→」といったかんじの看板を設置したいと思うんですけど、これを英語で作るとどうなるでしょうか?
この表現は日本語にはない英語の比喩表現として、私は大変気に入っています。
英語を学習する上で不可欠な、不規則動詞の活用。
中学生が英語を学ぶ際の最難関の一つが関係代名詞です。
TOEICの学習に限らず、英語を学ぶのであれば英和辞典は必携の書の一つでしょう。
アフィ狙いの釣り記事ですね。英語関係のコミュのあちこちにマルチポストしています。
まるでそれが正しい英語の証左かのように。
「まがりなりにも通じている」ということと「正しい英語を使っている」ということには雲泥の差があります。
上級者が英語力(特に読解力)をつけたければ、語彙を増やすのが王道ですね。
その当時、どこの公立の学校にもネイティブの英語の先生などいませんでした。
彼女は兄に勝るとも劣らぬくらい英語が上手だ。
彼女は英語を話すのが得意で、兄に勝るとも劣らぬくらいだ。
日本では、英語から日本語(英日)、日本語から英語(日英)への翻訳が多く、日西、西日の実需があまりありません。
英語には8つの主な品詞があります:名詞、動詞、形容詞、副詞、代名詞、前置詞、接続詞そして感嘆詞。
ちなみに私は英語がからきし駄目なんです。
話をすることで自分を曝け出すことを恐れず、英語で他人としゃべるあらゆる機会をとらえなさい。そうすればじきに形式張らない会話の場面で気楽になれるであろう。
鈴木先生は私たちに英語を教えてくれる。
例えば、君は英語が好きですか。
良い英語と、悪い英語はどのようにして区別できますか。