随机维基百科文章生成器
A Random Wikipedia Article Generator
我创建了这个随机维基百科文章生成器,但我希望它能生成一篇关于特定主题的文章,但出于某种原因,它生成的文章与我输入的主题无关。
代码:
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")
根据:https://en.wikipedia.org/wiki/Wikipedia:Special:Random
分类随机对象的终点是:
https://en.wikipedia.org/wiki/Special:RandomInCategory/
而不是:
https://en.wikipedia.org/wiki/Special:Random/
所以您应该将 url 更改为:
url = requests.get(f"https://en.wikipedia.org/wiki/Special:RandomInCategory/{topic}")
我创建了这个随机维基百科文章生成器,但我希望它能生成一篇关于特定主题的文章,但出于某种原因,它生成的文章与我输入的主题无关。
代码:
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")
根据:https://en.wikipedia.org/wiki/Wikipedia:Special:Random
分类随机对象的终点是: https://en.wikipedia.org/wiki/Special:RandomInCategory/
而不是: https://en.wikipedia.org/wiki/Special:Random/
所以您应该将 url 更改为:
url = requests.get(f"https://en.wikipedia.org/wiki/Special:RandomInCategory/{topic}")