|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from bs4 import BeautifulSoup, NavigableString\n", |
| 10 | + "\n", |
| 11 | + "html_doc = \"\"\"\n", |
| 12 | + " <pre\n", |
| 13 | + " style=\"\n", |
| 14 | + " box-sizing: border-box;\n", |
| 15 | + " font-family: 'Roboto Mono', sfmono-regular,\n", |
| 16 | + " consolas, 'liberation mono', menlo, courier,\n", |
| 17 | + " monospace;\n", |
| 18 | + " background: rgb(32, 33, 35) none repeat scroll\n", |
| 19 | + " 0% 0% / auto padding-box border-box;\n", |
| 20 | + " color: rgb(255, 255, 255);\n", |
| 21 | + " overflow-x: auto;\n", |
| 22 | + " border-bottom-left-radius: 4px;\n", |
| 23 | + " border-bottom-right-radius: 4px;\n", |
| 24 | + " margin: 0px;\n", |
| 25 | + " min-height: 44px;\n", |
| 26 | + " padding: 12px 16px;\n", |
| 27 | + " font-size: 15px;\n", |
| 28 | + " line-height: 24px;\n", |
| 29 | + " border-top-left-radius: 4px;\n", |
| 30 | + " border-top-right-radius: 4px;\n", |
| 31 | + " \"\n", |
| 32 | + " ><code style=\"box-sizing:border-box;font-family:"Roboto Mono", sfmono-regular, consolas, "liberation mono", menlo, courier, monospace;white-space:pre;\">\n", |
| 33 | + " <code style=\"box-sizing:border-box;float:left;font-family:"Roboto Mono", sfmono-regular, consolas, "liberation mono", menlo, courier, monospace;padding-right:16px;\"><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">1\n", |
| 34 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">2\n", |
| 35 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">3\n", |
| 36 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">4\n", |
| 37 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">5\n", |
| 38 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">6\n", |
| 39 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">7\n", |
| 40 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">8\n", |
| 41 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">9\n", |
| 42 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">10\n", |
| 43 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">11\n", |
| 44 | + "</span><span style=\"box-sizing:border-box;color:rgb(110, 110, 128);\">12\n", |
| 45 | + "</span></code>\n", |
| 46 | + "<span style=\"box-sizing:border-box;color:rgba(255, 255, 255, 0.5);\"># Note: you need to be using OpenAI Python v0.27.0 for the code below to work</span><span style=\"box-sizing:border-box;\"\n", |
| 47 | + "/><span style=\"box-sizing:border-box;\"/><span style=\"box-sizing:border-box;color:rgb(46, 149, 211);\">import</span><span style=\"box-sizing:border-box;\"> openai\n", |
| 48 | + "</span>\n", |
| 49 | + "openai.ChatCompletion.create(\n", |
| 50 | + "<span style=\"box-sizing:border-box;\"> model=</span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"gpt-3.5-turbo\"</span><span style=\"box-sizing:border-box;\">,\n", |
| 51 | + "</span> messages=[\n", |
| 52 | + "<span style=\"box-sizing:border-box;\"> {</span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"role\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"system\"</span><span style=\"box-sizing:border-box;\">, </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"content\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"You are a helpful assistant.\"</span><span style=\"box-sizing:border-box;\">},\n", |
| 53 | + "</span><span style=\"box-sizing:border-box;\"> {</span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"role\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"user\"</span><span style=\"box-sizing:border-box;\">, </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"content\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"Who won the world series in 2020?\"</span><span style=\"box-sizing:border-box;\">},\n", |
| 54 | + "</span><span style=\"box-sizing:border-box;\"> {</span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"role\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"assistant\"</span><span style=\"box-sizing:border-box;\">, </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"content\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"The Los Angeles Dodgers won the World Series in 2020.\"</span><span style=\"box-sizing:border-box;\">},\n", |
| 55 | + "</span><span style=\"box-sizing:border-box;\"> {</span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"role\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"user\"</span><span style=\"box-sizing:border-box;\">, </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"content\"</span><span style=\"box-sizing:border-box;\">: </span><span style=\"box-sizing:border-box;color:rgb(0, 166, 125);\">\"Where was it played?\"</span><span style=\"box-sizing:border-box;\">}\n", |
| 56 | + "</span> ]\n", |
| 57 | + ")</code></pre>\n", |
| 58 | + "\"\"\"\n", |
| 59 | + "soup = BeautifulSoup(html_doc, 'html.parser')\n", |
| 60 | + "\n", |
| 61 | + "# 找到所有的<pre>标签\n", |
| 62 | + "pre_tags = soup.find_all('pre')\n", |
| 63 | + "\n", |
| 64 | + "for pre in pre_tags:\n", |
| 65 | + " # 在每个<pre>标签中找到<code>标签\n", |
| 66 | + " code_tags = pre.find_all('code')\n", |
| 67 | + " \n", |
| 68 | + " for code in code_tags:\n", |
| 69 | + " # 检查<code>标签是否包含行号,这里假设行号是在<span>标签中的数字\n", |
| 70 | + " span_tags = code.find_all('span')\n", |
| 71 | + " \n", |
| 72 | + " for span in span_tags:\n", |
| 73 | + " if span.string and span.string.strip().isdigit():\n", |
| 74 | + " # 如果是行号,则删除这个<span>标签\n", |
| 75 | + " span.decompose()\n", |
| 76 | + "\n", |
| 77 | + "# 这时,soup中的HTML已经没有行号了\n", |
| 78 | + "print(soup.prettify())\n" |
| 79 | + ] |
| 80 | + } |
| 81 | + ], |
| 82 | + "metadata": { |
| 83 | + "kernelspec": { |
| 84 | + "display_name": "notion", |
| 85 | + "language": "python", |
| 86 | + "name": "python3" |
| 87 | + }, |
| 88 | + "language_info": { |
| 89 | + "codemirror_mode": { |
| 90 | + "name": "ipython", |
| 91 | + "version": 3 |
| 92 | + }, |
| 93 | + "file_extension": ".py", |
| 94 | + "mimetype": "text/x-python", |
| 95 | + "name": "python", |
| 96 | + "nbconvert_exporter": "python", |
| 97 | + "pygments_lexer": "ipython3", |
| 98 | + "version": "3.11.2" |
| 99 | + }, |
| 100 | + "orig_nbformat": 4 |
| 101 | + }, |
| 102 | + "nbformat": 4, |
| 103 | + "nbformat_minor": 2 |
| 104 | +} |
0 commit comments