简体   繁体   English

如何在 HTML 页面中显示语法突出显示的 Python 代码?

[英]How can I show syntax-highlighted Python code in a HTML-page?

Is it somehow possible to show syntax-highlighted python code in a webpage?是否有可能在网页中显示语法突出显示的 python 代码?

I found this:我找到了这个:

<pre class="brush: python">
    # python code here
</pre>

However, it shows all the code in black.但是,它以黑色显示所有代码。 I want import to be orange, strings to be green.我希望import为橙色,字符串为绿色。

Is it possible to do this?是否有可能做到这一点?

Thank you!谢谢!

I use prism.js我使用prism.js

It looks Like this, it has many features它看起来像这样,它有很多功能

在此处输入图像描述

If you wish to only display code, python in this case, consider using Github gist .如果您只想显示代码 python 在这种情况下,请考虑使用 Github gist

You can then embed it using the 'embed' option on the top right corner.然后,您可以使用右上角的“嵌入”选项嵌入它。 It will give you a script tag that you can copy and add to your webpage like so:它将为您提供一个脚本标签,您可以将其复制并添加到您的网页中,如下所示:

<script src="https://gist.github.com/username/a39a422ebdff6e732753b90573100b16.js"></script>

刮<div<span from html-page< div><div id="text_translate"><p> 我正在尝试使用 Eclipse 中的 Python 创建一个简单的天气预报。 到目前为止,我已经写了这个:</p><pre> from bs4 import BeautifulSoup import requests def weather_forecast(): url = 'https://www.yr.no/nb/v%C3%A6rvarsel/daglig-tabell/1-92416/Norge/Vestland/Bergen/Bergen' r = requests.get(url) # Get request for contents of the page print(r.content) # Outputs HTML code for the page soup = BeautifulSoup(r.content, 'html5lib') # Parse the data with BeautifulSoup(HTML-string, html-parser) min_max = soup.select('min-max.temperature') # Select all spans with a "min-max-temperature" attribute print(min_max.prettify()) table = soup.find('div', attrs={'daily-weather-list-item__temperature'}) print(table.prettify())</pre><p> 从具有如下元素的 html 页面:</p><p> <a href="https://i.stack.imgur.com/liV2d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/liV2d.png" alt=""></a></p><p> 我在 HTML 页面的元素中找到了第一个温度的路径,但是当我尝试执行我的代码并打印以查看我是否正确完成时,没有打印任何内容。 我的目标是打印一张带有日期和相应温度的表格,这似乎是一项简单的任务,但我不知道如何正确命名属性或如何在一次迭代中从 HTML 页面中将它们全部刮掉。</p><p> &lt;span 存储了两个温度,一个最小值和一个最大值,这里只是碰巧它们是相同的。</p><p> 我想将 go 放入每个 &lt;div class="daily-weather-list-item__temperature" 中,收集两个温度并将它们添加到字典中,我该怎么做?</p><p> 我已经在 stackoverflow 上查看了这个问题,但我无法弄清楚: <a href="https://stackoverflow.com/questions/53084902/python-beautifulsoup-scraping-div-spans-and-p-tags-also-how-to-get-exact-mat" rel="nofollow noreferrer">Python BeautifulSoup - Scraping Div Spans 和 p 标签 - 以及如何在 div 名称上获得完全匹配</a></p></div></div<span> - Scrape <div<span from HTML-page

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在 HTML 页面上运行 Python 代码吗? 如何? - Can run Python code on a HTML page? How? 刮<div<span from html-page< div><div id="text_translate"><p> 我正在尝试使用 Eclipse 中的 Python 创建一个简单的天气预报。 到目前为止,我已经写了这个:</p><pre> from bs4 import BeautifulSoup import requests def weather_forecast(): url = 'https://www.yr.no/nb/v%C3%A6rvarsel/daglig-tabell/1-92416/Norge/Vestland/Bergen/Bergen' r = requests.get(url) # Get request for contents of the page print(r.content) # Outputs HTML code for the page soup = BeautifulSoup(r.content, 'html5lib') # Parse the data with BeautifulSoup(HTML-string, html-parser) min_max = soup.select('min-max.temperature') # Select all spans with a "min-max-temperature" attribute print(min_max.prettify()) table = soup.find('div', attrs={'daily-weather-list-item__temperature'}) print(table.prettify())</pre><p> 从具有如下元素的 html 页面:</p><p> <a href="https://i.stack.imgur.com/liV2d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/liV2d.png" alt=""></a></p><p> 我在 HTML 页面的元素中找到了第一个温度的路径,但是当我尝试执行我的代码并打印以查看我是否正确完成时,没有打印任何内容。 我的目标是打印一张带有日期和相应温度的表格,这似乎是一项简单的任务,但我不知道如何正确命名属性或如何在一次迭代中从 HTML 页面中将它们全部刮掉。</p><p> &lt;span 存储了两个温度,一个最小值和一个最大值,这里只是碰巧它们是相同的。</p><p> 我想将 go 放入每个 &lt;div class="daily-weather-list-item__temperature" 中,收集两个温度并将它们添加到字典中,我该怎么做?</p><p> 我已经在 stackoverflow 上查看了这个问题,但我无法弄清楚: <a href="https://stackoverflow.com/questions/53084902/python-beautifulsoup-scraping-div-spans-and-p-tags-also-how-to-get-exact-mat" rel="nofollow noreferrer">Python BeautifulSoup - Scraping Div Spans 和 p 标签 - 以及如何在 div 名称上获得完全匹配</a></p></div></div<span> - Scrape <div<span from HTML-page 如何在我想要的 html 页面中显示 Django 模型? - How can I show the Django model in the html page I want? 如何修复python代码中的语法错误? - How can I fix syntax-error in python code? 如何在 VS 代码中自定义 python 语法突出显示? - How can I customize python syntax highlighting in VS code? 如何在我的 Python 代码中跟踪语法错误? - How can I trace a syntax error in my Python code? 如何解决Python 2.7中以下代码的语法错误? - How can I solve syntax error for the following code in Python 2.7? 如何在 python 中获取我在 Firefox 上打开的页面的 HTML 代码源 - How can I get HTML code source of my opened page on Firefox in python 如何在html页面上单击按钮执行python代码,并在Django的同一页面上显示结果? - How to execute a python code on clicking a button on html page and show the results on the same page in Django? 使用转义的 ascii 字符串正确解析 html 页面 - Proper parsing of a html-page with escaped ascii-strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM