To modify the WikiQuiz application to generate questions and answers via a web link instead of a Wikipedia article, we need to make the following changes:
Modify the Article class in Article.py to accept a URL instead of an article name.
We will use the requests and BeautifulSoup libraries to fetch and parse the HTML content of the webpage.
2. **Modify the `generate_questions` method in `Article.py` to extract text from the webpage.**
We will use the `BeautifulSoup` library to extract all the paragraphs from the webpage and then generate questions from the text.
```python
def generate_questions(self):
paragraphs = self.soup.find_all('p')
text = ''
for paragraph in paragraphs:
text += paragraph.get_text() + ' '
self.generate_questions_for(text)
Modify the server.py to accept a URL instead of an article name.
We will modify the /quiz endpoint to accept a URL as a parameter.
python
@app.route('/quiz/path:url/', methods=['GET'])
def get_quiz(url):
a = Article(url)
# rest of the code remains the same
code
4. **Modify the `script.js` to send the URL to the server.**
We will modify the `new_article_query` function to send the URL to the server.
```javascript
function new_article_query(_data) {
if (!_data) {
make_request(search.value, new_article_query);
curQuery = search.value;
} else {
// rest of the code remains the same
Modify the index.html to input a URL instead of an article name.
We will modify the input field to accept a URL.
html
Here is the modified code:
Article.py
python
import requests
from bs4 import BeautifulSoup
from Quiz import Quiz
class Article:
def __init__(self, url):
self.url = url
self.page = requests.get(url)
self.soup = BeautifulSoup(self.page.content, 'html.parser')
self.quiz = Quiz([])
self.generate_questions()
def generate_questions(self):
paragraphs = self.soup.find_all('p')
text = ''
for paragraph in paragraphs:
text += paragraph.get_text() + ' '
self.generate_questions_for(text)
# rest of the code remains the same
server.py
python
from flask import Flask, request, Response
from Article import Article
app = Flask(__name__)
@app.route('/quiz/<path:url>/', methods=['GET'])
def get_quiz(url):
a = Article(url)
# rest of the code remains the same
script.js
javascript
function new_article_query(_data) {
if (!_data) {
make_request(search.value, new_article_query);
curQuery = search.value;
} else {
// rest of the code remains the same