?,python,beautifulsoup,css-selectors"/>

首页 > 解决方案 > 如何找到最近的兄弟姐妹

  • 对于每个
  • ?
  • 问题描述

    标签: pythonbeautifulsoupcss-selectors

    解决方案


    您可以使用.find_previous('h4')

    import requests
    from bs4 import BeautifulSoup
    
    
    url = "https://french.kwiziq.com/revision/grammar"
    
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    for a in soup.select(".callout  li > a:nth-of-type(1)"):
        print(
            "{:<70} {}".format(
                a.get_text(strip=True), a.find_previous("h4").get_text(strip=True)
            )
        )
    

    印刷:

    Saying your name: Je m'appelle, Tu t'appelles, Vous vous appelez       A0: Pronouns
    Tu and vous are used for three types of you                            A0: Pronouns
    Je becomes j' with verbs beginning with a vowel (elision)              A0: Verbs Tenses & Conjugation
    J'habite à [city] = I live in [city]                                   A0: Idioms, Idiomatic Usage, and Structures
    Je viens de + [city] = I'm from + [city]                               A0: Idioms, Idiomatic Usage, and Structures
    Conjugate être (je suis, tu es, vous êtes) in Le Présent (present tense) A0: Verbs Tenses & Conjugation
    Make most adjectives feminine by adding -e                             A0: Adjectives & Adverbs
    Nationalities differ depending on whether you're a man or a woman (adjectives) A0: Adjectives & Adverbs
    Conjugate avoir (j'ai, tu as, vous avez) in Le Présent (present tense) A0: Verbs Tenses & Conjugation
    Using un, une to say "a" (indefinite articles)                         A0: Nouns & Articles
    
    ...
    
    French vocabulary and grammar lists by theme                           C1: Idioms, Idiomatic Usage, and Structures
    French Fill-in-the-Blanks Tests                                        C1: Idioms, Idiomatic Usage, and Structures
    

    推荐阅读