Python 3.x 如果我没有';没有确切的字符串,但有一个包含它的变量

Python 3.x 如果我没有';没有确切的字符串,但有一个包含它的变量,python-3.x,string,web-scraping,replace,beautifulsoup,Python 3.x,String,Web Scraping,Replace,Beautifulsoup,所以,作为课程的一部分,我抓取了一个网站作为作业,并创建了一个猜谜游戏来猜猜引用语的作者。游戏提供了四个猜测,在最后一个猜测中,我想提供作者个人简历中的一句话。现在,我已经成功地请求了这个页面,并删除了个人简历,但我想从个人简历的描述中删除所有作者姓名的实例,这样我就可以避免在最后的提示中给出作者的姓名 elif remaining_guesses == 1: bio_request = requests.get(f"{base_url}{random_quot

所以,作为课程的一部分,我抓取了一个网站作为作业,并创建了一个猜谜游戏来猜猜引用语的作者。游戏提供了四个猜测,在最后一个猜测中,我想提供作者个人简历中的一句话。现在,我已经成功地请求了这个页面,并删除了个人简历,但我想从个人简历的描述中删除所有作者姓名的实例,这样我就可以避免在最后的提示中给出作者的姓名

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()函数,但我知道不能通过将变量作为字符串进行替换来使用它,因此如何用例如“author”来替换出现的名称、姓氏或名字


请让我知道,如果我还不够清楚,我甚至可以提供我的全部代码。我在考虑使用正则表达式的可能性,但我不确定这是否最合适。所以,问题是我每次在游戏循环时都随机选取引号,所以我不知道作者的名字,我可以像使用random_quote[“name”]一样将其写入变量,但replace()需要精确的输入,我可以给它一个变量输入,如果位于描述部分,则替换变量的所有实例,这是作者简历中的一句话(在本例中为第5句,但我可能会将其随机设置,这无关紧要)。下面是一个例子,展示了简历的样子。我希望我能正确理解你的问题,但你可以在全文中替换姓名(全名、第一名、第二名),然后随机选择句子

例如:

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

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

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")
我不确定这是否有帮助。如果他们使用“Mr.”或类似的词,那么您也可以在同一循环中添加该情况。不管是什么,一定要让我知道:祝你好运