英文:
I have a hard time finding how to display a python code in html
问题
抱歉,我只能帮助你翻译文本,而不能执行代码或提供代码解决方案。如果你需要帮助翻译代码中的注释或说明,请提供相关文本。
英文:
Hi, I'm Currently programming a website for displaying all the previous year questions of a competitive exam in India name JEE. For getting the Questions and their Respective Options I used bs4 and requests to web scrape from a website named ExamSide where all these questions and options are available it was successful while printing it in Python Prompt(i have given the code below) but I have hard time finding how to display this stuffs in my html page via django or flask
It would be Really Helpful If you Guys Help me Out
Thanks in Advance
import requests
from bs4 import BeautifulSoup
webpage_codes = ['lgnz9975', 'lgnzeywd', 'lgnzj56q', 'lgnzt580', 'lgnw7ha3', 'lgnwmpwc','lgnx2ht3', 'lgnx23k6', 'lgny1m5w', 'lgny7rsq']
i = 0
hello = 1
print("JEE MAINS 15th Morning Session")
print("Difficulty : Medium")
while i < len(webpage_codes):
r = requests.get("https://questions.examside.com/past-years/year-wise/jee/jee-main/jee-main-2023-online-15th-april-morning-shift/"+webpage_codes[i])
soup = BeautifulSoup(r.content, 'html.parser')
questions = []
options = []
s = soup.find('div', class_ = "flex-grow flex flex-col gap-2 lg:gap-4")
result_set = s.find_all('div')
for h in result_set:
if h.get('class') == ['question', 'xl:text-lg', 'px-4', 'py-2.5']:
questions += [h.text]
for a in result_set:
if a.get('class') == ['grow', 'question', 'xl:text-lg']:
options += [a.text]
for z in range(0, len(options)):
options[z] = options[z].replace("$", "$$")
for y in range(0, len(questions)):
questions[y] = questions[y].replace("$", "$$")
w = 0
for c in questions:
if options[w] == '':
pass
else:
print(str(hello)+".", end = "")
print(c)
print("A.",options[w])
w += 1
print("B.",options[w])
w += 1
print("C.",options[w])
w += 1
print("D.",options[w])
w += 1
print("\n\n")
hello += 1
i = i+ 1
答案1
得分: 0
Sure, here's the translated code:
是的,你可以试试吗?
import requests
from bs4 import BeautifulSoup
from django.shortcuts import render
def display_questions(request):
webpage_codes = ['lgnz9975', 'lgnzeywd', 'lgnzj56q', 'lgnzt580', 'lgnw7ha3', 'lgnwmpwc', 'lgnx2ht3', 'lgnx23k6', 'lgny1m5w', 'lgny7rsq']
i = 0
hello = 1
data = []
while i < len(webpage_codes):
r = requests.get("https://questions.examside.com/past-years/year-wise/jee/jee-main/jee-main-2023-online-15th-april-morning-shift/" + webpage_codes[i])
soup = BeautifulSoup(r.content, 'html.parser')
questions = []
options = []
s = soup.find('div', class_="flex-grow flex flex-col gap-2 lg:gap-4")
result_set = s.find_all('div')
for h in result_set:
if h.get('class') == ['question', 'xl:text-lg', 'px-4', 'py-2.5']:
questions += [h.text]
for a in result_set:
if a.get('class') == ['grow', 'question', 'xl:text-lg']:
options += [a.text]
for z in range(0, len(options)):
options[z] = options[z].replace("$", "$$")
for y in range(0, len(questions)):
questions[y] = questions[y].replace("$", "$$")
w = 0
for c in questions:
if options[w] == '':
pass
else:
question_data = {
'number': hello,
'question': c,
'option_a': options[w],
'option_b': options[w+1],
'option_c': options[w+2],
'option_d': options[w+3],
}
data.append(question_data)
w += 4
hello += 1
i = i + 1
return render(request, 'questions.html', {'data': data})
Please note that I've retained the code structure and variable names as-is while providing the translation.
英文:
Yes, can you try it?
import requests
from bs4 import BeautifulSoup
from django.shortcuts import render
def display_questions(request):
webpage_codes = ['lgnz9975', 'lgnzeywd', 'lgnzj56q', 'lgnzt580', 'lgnw7ha3', 'lgnwmpwc','lgnx2ht3', 'lgnx23k6', 'lgny1m5w', 'lgny7rsq']
i = 0
hello = 1
data = []
while i < len(webpage_codes):
r = requests.get("https://questions.examside.com/past-years/year-wise/jee/jee-main/jee-main-2023-online-15th-april-morning-shift/"+webpage_codes[i])
soup = BeautifulSoup(r.content, 'html.parser')
questions = []
options = []
s = soup.find('div', class_ = "flex-grow flex flex-col gap-2 lg:gap-4")
result_set = s.find_all('div')
for h in result_set:
if h.get('class') == ['question', 'xl:text-lg', 'px-4', 'py-2.5']:
questions += [h.text]
for a in result_set:
if a.get('class') == ['grow', 'question', 'xl:text-lg']:
options += [a.text]
for z in range(0, len(options)):
options[z] = options[z].replace("$", "$$")
for y in range(0, len(questions)):
questions[y] = questions[y].replace("$", "$$")
w = 0
for c in questions:
if options[w] == '':
pass
else:
question_data = {
'number': hello,
'question': c,
'option_a': options[w],
'option_b': options[w+1],
'option_c': options[w+2],
'option_d': options[w+3],
}
data.append(question_data)
w += 4
hello += 1
i = i + 1
return render(request, 'questions.html', {'data': data})
And the html file
<!DOCTYPE html>
<html>
<head>
<title>JEE Questions</title>
</head>
<body>
<h1>JEE Questions</h1>
<table>
<tr>
<th>Number</th>
<th>Question</th>
<th>Option A</th>
<th>Option B</th>
<th>Option C</th>
<th>Option D</th>
</tr>
{% for question in data %}
<tr>
<td>{{ question.number }}</td>
<td>{{ question.question }}</td>
<td>{{ question.option_a }}</td>
<td>{{ question.option_b }}</td>
<td>{{ question.option_c }}</td>
<td>{{ question.option_d }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论