繁体   English   中英

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

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

是否有可能在网页中显示语法突出显示的 python 代码?

我找到了这个:

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

但是,它以黑色显示所有代码。 我希望import为橙色,字符串为绿色。

是否有可能做到这一点?

谢谢!

我使用prism.js

它看起来像这样,它有很多功能

在此处输入图像描述

如果您只想显示代码 python 在这种情况下,请考虑使用 Github gist

然后,您可以使用右上角的“嵌入”选项嵌入它。 它将为您提供一个脚本标签,您可以将其复制并添加到您的网页中,如下所示:

<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 代码吗? 如何? 刮<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> 如何在我想要的 html 页面中显示 Django 模型? 如何修复python代码中的语法错误? 如何在 VS 代码中自定义 python 语法突出显示? 如何在我的 Python 代码中跟踪语法错误? 如何解决Python 2.7中以下代码的语法错误? 如何在 python 中获取我在 Firefox 上打开的页面的 HTML 代码源 如何在html页面上单击按钮执行python代码,并在Django的同一页面上显示结果? 使用转义的 ascii 字符串正确解析 html 页面
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM