【发布时间】:2021-07-30 20:24:09
【问题描述】:
我有这个我创建的随机维基百科文章生成器,但我希望它会生成一篇关于特定主题的文章,并且由于某种原因,它会生成文章但不是关于我输入的主题的文章。
代码:
import requests
from bs4 import BeautifulSoup
import webbrowser
while True:
topic = input("Plz enter a topic you would like to read about: ")
url = requests.get(f"https://en.wikipedia.org/wiki/Special:Random/{topic}")
soup = BeautifulSoup(url.content, "html.parser")
title = soup.find(class_="firstHeading").text
answer = input(f"The article is: {title} \nWould you like to view this article? (Y/N)\n").upper()
if answer == "Y":
url = "https://en.wikipedia.org/wiki\%s" %title
webbrowser.open(url)
leave = input("Would you like to read another one? (Y/N)\n").upper()
if leave == "Y":
pass
else:
break
elif answer == "N":
print("Try again!!")
else:
print("I don't know what it is")
【问题讨论】: