我有困难找到如何在HTML中显示Python代码。

huangapple go评论131阅读模式
英文:

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 = [&#39;lgnz9975&#39;, &#39;lgnzeywd&#39;, &#39;lgnzj56q&#39;, &#39;lgnzt580&#39;, &#39;lgnw7ha3&#39;, &#39;lgnwmpwc&#39;,&#39;lgnx2ht3&#39;, &#39;lgnx23k6&#39;, &#39;lgny1m5w&#39;, &#39;lgny7rsq&#39;]
i = 0
hello = 1
data = []
while i &lt; len(webpage_codes):
r = requests.get(&quot;https://questions.examside.com/past-years/year-wise/jee/jee-main/jee-main-2023-online-15th-april-morning-shift/&quot;+webpage_codes[i])
soup = BeautifulSoup(r.content, &#39;html.parser&#39;)
questions = []
options = []
s = soup.find(&#39;div&#39;, class_ = &quot;flex-grow flex flex-col gap-2 lg:gap-4&quot;)
result_set = s.find_all(&#39;div&#39;)
for h in result_set:
if h.get(&#39;class&#39;) == [&#39;question&#39;, &#39;xl:text-lg&#39;, &#39;px-4&#39;, &#39;py-2.5&#39;]:
questions += [h.text]
for a in result_set:
if a.get(&#39;class&#39;) == [&#39;grow&#39;, &#39;question&#39;, &#39;xl:text-lg&#39;]:
options += [a.text]
for z in range(0, len(options)):
options[z] = options[z].replace(&quot;$&quot;, &quot;$$&quot;)
for y in range(0, len(questions)):
questions[y] = questions[y].replace(&quot;$&quot;, &quot;$$&quot;)
w = 0
for c in questions:
if options[w] == &#39;&#39;:
pass
else:
question_data = {
&#39;number&#39;: hello,
&#39;question&#39;: c,
&#39;option_a&#39;: options[w],
&#39;option_b&#39;: options[w+1],
&#39;option_c&#39;: options[w+2],
&#39;option_d&#39;: options[w+3],
}
data.append(question_data)
w += 4
hello += 1
i = i + 1
return render(request, &#39;questions.html&#39;, {&#39;data&#39;: data})

And the html file

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;JEE Questions&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;JEE Questions&lt;/h1&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;Number&lt;/th&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Option A&lt;/th&gt;
&lt;th&gt;Option B&lt;/th&gt;
&lt;th&gt;Option C&lt;/th&gt;
&lt;th&gt;Option D&lt;/th&gt;
&lt;/tr&gt;
{% for question in data %}
&lt;tr&gt;
&lt;td&gt;{{ question.number }}&lt;/td&gt;
&lt;td&gt;{{ question.question }}&lt;/td&gt;
&lt;td&gt;{{ question.option_a }}&lt;/td&gt;
&lt;td&gt;{{ question.option_b }}&lt;/td&gt;
&lt;td&gt;{{ question.option_c }}&lt;/td&gt;
&lt;td&gt;{{ question.option_d }}&lt;/td&gt;
&lt;/tr&gt;
{% endfor %}
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年5月25日 19:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定