BeautifulSoup продолжает заменять '<' на <после того, как я предоставлю ему необработанную строку HTML - PullRequest
0 голосов
/ 05 марта 2020

Я сделал запрос к API и в итоге получил следующее:

&lt;!--START OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD --&gt;
&lt;!-----------------------------&gt;

&lt;div align=&#034;center&#034; id=&#034;Storage Report HTML Generator_25382&#034; x:publishsource=&#034;Excel&#034;&gt;

&lt;table width=&#034;1269&#034; style=&#034;width: 951pt; border-collapse: collapse; table-layout: fixed;&#034; border=&#034;0&#034; cellspacing=&#034;0&#034; cellpadding=&#034;0&#034;&gt;
 &lt;colgroup&gt;&lt;col width=&#034;185&#034; style=&#034;width: 139pt; mso-width-source: userset; mso-width-alt: 6570;&#034;&gt;
 &lt;col width=&#034;91&#034; style=&#034;width: 68pt; mso-width-source: userset; mso-width-alt: 3242;&#034;&gt;
 &lt;col width=&#034;106&#034; style=&#034;width: 79pt; mso-width-source: userset; mso-width-alt: 3754;&#034;&gt;
 &lt;col width=&#034;94&#034; style=&#034;width: 70pt; mso-width-source: userset; mso-width-alt: 3328;&#034;&gt;
 &lt;col width=&#034;150&#034; style=&#034;width: 113pt; mso-width-source: userset; mso-width-alt: 5347;&#034;&gt;
 &lt;col width=&#034;195&#034; style=&#034;width: 146pt; mso-width-source: userset; mso-width-alt: 6940;&#034;&gt;
 &lt;col width=&#034;64&#034; style=&#034;width: 48pt;&#034; span=&#034;7&#034;&gt;
 &lt;/colgroup&gt;&lt;tbody&gt;&lt;tr height=&#034;21&#034; style=&#034;height: 15.6pt;&#034;&gt;
  &lt;td width=&#034;276&#034; height=&#034;21&#034; class=&#034;xl9825382&#034; style=&#034;width: 207pt; height: 15.6pt;&#034; colspan=&#034;2&#034;&gt;Energy Information Administration&lt;/td&gt;      
  &lt;td width=&#034;106&#034; class=&#034;xl9925382&#034; style=&#034;width: 79pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;94&#034; class=&#034;xl9925382&#034; style=&#034;width: 70pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;150&#034; class=&#034;xl6725382&#034; style=&#034;width: 113pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;195&#034; class=&#034;xl6725382&#034; style=&#034;width: 146pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
  &lt;td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;&gt;&lt;/td&gt;
 &lt;/tr&gt;

Это всего лишь фрагмент, очевидно, эта часть страницы была сгенерирована с помощью Excel, поэтому она не форматируется должным образом (во всяком случае, чтобы избежать получения этой информации с запросами в правильном HTML формате, также приветствуется)

Я просто заменил &lt и &gt на < и < соответственно на all_data_html = all_data_html.replace("&lt;", "<").replace("&gt;", ">").replace(";", ">"). Что на самом деле делает его чистым:

 <!----------------------------->
<!--START OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD -->
<!----------------------------->

<div align=&#034;center&#034; id=&#034;Storage Report HTML Generator_25382&#034; x:publishsource=&#034;Excel&#034;>

<table width=&#034;1269&#034; style=&#034;width: 951pt; border-collapse: collapse; table-layout: fixed;&#034; border=&#034;0&#034; cellspacing=&#034;0&#034; cellpadding=&#034;0&#034;>
 <colgroup><col width=&#034;185&#034; style=&#034;width: 139pt; mso-width-source: userset; mso-width-alt: 6570;&#034;>
 <col width=&#034;91&#034; style=&#034;width: 68pt; mso-width-source: userset; mso-width-alt: 3242;&#034;>
 <col width=&#034;106&#034; style=&#034;width: 79pt; mso-width-source: userset; mso-width-alt: 3754;&#034;>
 <col width=&#034;94&#034; style=&#034;width: 70pt; mso-width-source: userset; mso-width-alt: 3328;&#034;>
 <col width=&#034;150&#034; style=&#034;width: 113pt; mso-width-source: userset; mso-width-alt: 5347;&#034;>
 <col width=&#034;195&#034; style=&#034;width: 146pt; mso-width-source: userset; mso-width-alt: 6940;&#034;>
 <col width=&#034;64&#034; style=&#034;width: 48pt;&#034; span=&#034;7&#034;>
 </colgroup><tbody><tr height=&#034;21&#034; style=&#034;height: 15.6pt;&#034;>
  <td width=&#034;276&#034; height=&#034;21&#034; class=&#034;xl9825382&#034; style=&#034;width: 207pt; height: 15.6pt;&#034; colspan=&#034;2&#034;>Energy Information Administration</td>
  <td width=&#034;106&#034; class=&#034;xl9925382&#034; style=&#034;width: 79pt;&#034;></td>
  <td width=&#034;94&#034; class=&#034;xl9925382&#034; style=&#034;width: 70pt;&#034;></td>
  <td width=&#034;150&#034; class=&#034;xl6725382&#034; style=&#034;width: 113pt;&#034;></td>
  <td width=&#034;195&#034; class=&#034;xl6725382&#034; style=&#034;width: 146pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>
  <td width=&#034;64&#034; class=&#034;xl1525382&#034; style=&#034;width: 48pt;&#034;></td>

Однако, когда я создаю из него объект soup, затем печатаю его, символы возвращаются обратно к сущностям, и в первом случае это выглядит как HTML фрагмент: soup = BeautifulSoup(all_data_html, "html.parser")

Почему BeautifulSoup не распознает его как электронную почту? Это потому, что это переформатированная строка с невидимыми недостатками?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...