【问题标题】:How to replace a part of a string using Python3 if I don't have the exact string but a variable that contains it如果我没有确切的字符串但包含它的变量,如何使用 Python3 替换字符串的一部分
【发布时间】:2020-06-28 05:30:52
【问题描述】:

所以,我已经抓取了一个网站http://quotes.toscrape.com/ 作为课程的一部分,并正在创建一个猜谜游戏来猜测引用的作者。游戏提供了四个猜测,在最后一个猜测中,我想提供作者简历中的一句话。现在,我已成功请求此页面并抓取了简历,但我想从简历的描述中删除作者姓名、姓氏和名字的所有实例,以避免在最后提示中提供作者姓名。

elif remaining_guesses == 1:
            bio_request = requests.get(f"{base_url}{random_quote['url']}")
            soup = BeautifulSoup(bio_request.text, "html.parser")
            description = soup.find(class_="author-description").get_text()
            desc_part = description.split(".")[4]
            name = random_quote["name"]
            print(type(name))

            if name in desc_part:
                desc_part.replace(name, "author")
            elif random_quote["name"].split(" ")[0] in desc_part:
                desc_part.replace(random_quote["name"].split(" ")[0], "author")
            elif random_quote["name"].split(" ")[1] in desc_part:
                desc_part.replace(random_quote["name"].split(" ")[1], "author")
            print(f"A hint from his bio: {desc_part}")

我正在尝试使用 replace() 函数,但我知道我不能通过将变量作为字符串来替换来以这种方式使用它,那么如何替换姓名、姓氏或名字例如“作者”的出现。

如果我不够清楚,请告诉我,我什至可以提供我拥有的整个代码。我在考虑使用正则表达式的可能性,但我不确定这是否最合适。所以,问题是我每次在游戏循环时都会随机选择引号,所以我不知道作者的名字,我可以像使用 random_quote["name"] 一样将它写在变量中,但是替换() 需要一个精确的输入,我可以给它一个变量输入并说如果位于 desc_part 中的变量的所有实例,这是作者简历中的一个句子(在这种情况下是第 5 句,但我可能让它随机,它没有没关系)。以下是个人简介的示例http://quotes.toscrape.com/author/Albert-Einstein/

【问题讨论】:

    标签: python-3.x string web-scraping replace beautifulsoup


    【解决方案1】:

    我希望我能正确理解您的问题 - 但您可以在全文中替换 name(full, first, second),然后随机选择句子。

    例如:

    import random
    import requests
    from bs4 import BeautifulSoup
    from textwrap import wrap
    
    
    url = 'http://quotes.toscrape.com/author/Albert-Einstein/'
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    
    name = 'Albert Einstein'
    replaced_name = '*** REDACTED ***'
    desc = soup.select_one('.author-description').get_text(strip=True, separator=' ')
    
    # remove full name
    desc = desc.replace(name, replaced_name)
    
    # remove first name
    desc = desc.replace(name.split()[0], replaced_name)
    
    # remove last name
    desc = desc.replace(name.split()[1], replaced_name)
    
    print('\n'.join(wrap(desc)))
    
    # pick random sentence
    sentence = random.choice(desc.split('. '))
    print()
    print(sentence)
    

    打印:

    In 1879, *** REDACTED *** was born in Ulm, Germany. He completed his
    Ph.D. at the University of Zurich by 1909. His 1905 paper explaining
    the photoelectric effect, the basis of electronics, earned him the
    Nobel Prize in 1921. His first paper on Special Relativity Theory,
    also published in 1905, changed the world. After the rise of the Nazi
    party, *** REDACTED *** made Princeton his permanent home, becoming a
    U.S. citizen in 1940. *** REDACTED ***, a pacifist during World War I,
    stayed a firm proponent of social justice and responsibility. He
    chaired the Emergency Committee of Atomic Scientists, which organized
    to alert the public to the dangers of atomic warfare.At a symposium,
    he advised: "In their struggle for the ethical good, teachers of
    religion must have the stature to give up the doctrine of a personal
    God, that is, give up that source of fear and hope which in the past
    placed such vast power in the hands of priests. In their labors they
    will have to avail themselves of those forces which are capable of
    cultivating the Good, the True, and the Beautiful in humanity itself.
    This is, to be sure a more difficult but an incomparably more worthy
    task . . . " ("Science, Philosophy and Religion, A Symposium,"
    published by the Conference on Science, Philosophy and Religion in
    their Relation to the Democratic Way of Life, Inc., New York, 1941).
    In a letter to philosopher Eric Gutkind, dated Jan. 3, 1954, ***
    REDACTED *** stated: "The word god is for me nothing more than the
    expression and product of human weaknesses, the Bible a collection of
    honorable, but still primitive legends which are nevertheless pretty
    childish. No interpretation no matter how subtle can (for me) change
    this," (The Guardian, "Childish superstition: *** REDACTED ***'s
    letter makes view of religion relatively clear," by James Randerson,
    May 13, 2008). D. 1955.While best known for his mass–energy
    equivalence formula E = mc2 (which has been dubbed "the world's most
    famous equation"), he received the 1921 Nobel Prize in Physics "for
    his services to theoretical physics, and especially for his discovery
    of the law of the photoelectric effect". The latter was pivotal in
    establishing quantum theory.*** REDACTED *** thought that Newtonion
    mechanics was no longer enough to reconcile the laws of classical
    mechanics with the laws of the electromagnetic field. This led to the
    development of his special theory of relativity. He realized, however,
    that the principle of relativity could also be extended to
    gravitational fields, and with his subsequent theory of gravitation in
    1916, he published a paper on the general theory of relativity. He
    continued to deal with problems of statistical mechanics and quantum
    theory, which led to his explanations of particle theory and the
    motion of molecules. He also investigated the thermal properties of
    light which laid the foundation of the photon theory of light.He was
    visiting the United States when Adolf Hitler came to power in 1933 and
    did not go back to Germany. On the eve of World War II, he endorsed a
    letter to President Franklin D. Roosevelt alerting him to the
    potential development of "extremely powerful bombs of a new type" and
    recommending that the U.S. begin similar research. This eventually led
    to what would become the Manhattan Project. *** REDACTED *** supported
    defending the Allied forces, but largely denounced the idea of using
    the newly discovered nuclear fission as a weapon. Later, with Bertrand
    Russell, *** REDACTED *** signed the Russell–*** REDACTED ***
    Manifesto, which highlighted the danger of nuclear weapons. ***
    REDACTED *** was affiliated with the Institute for Advanced Study in
    Princeton, New Jersey, until his death in 1955.His great intellectual
    achievements and originality have made the word "*** REDACTED ***"
    synonymous with genius.More: http://en.wikipedia.org/wiki/*** REDACTED
    ***_E...http://www.nobelprize.org/nobel_prize...
    

    随机句:

    *** REDACTED ***, a pacifist during World War I, stayed a firm proponent of social justice and responsibility
    

    【讨论】:

      【解决方案2】:

      不确定这是否有帮助,但您可以这样做:

      name= "Albert Einstein"
      name_parts=name.split()
      scraped_data=scraped_data.replace(name, "Author")
      for i in name_parts:
         scraped_data=scraped_data.replace(i, "Author")
      

      我不确定这是否有帮助。如果他们使用“先生”。或类似的东西,那么您也可以在同一个循环中添加该案例。不管是什么,请告诉我:D 祝你好运!

      【讨论】:

        猜你喜欢
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-27
        • 1970-01-01
        • 1970-01-01
        • 2020-06-12
        • 2021-07-11
        相关资源
        最近更新 更多