使用BeautifulSoup抓取网站时显示符号

Shreya

我是网络爬虫的新手。我试图解决有关如何显示符号的问题。BeautifulSoup用于刮擦网站,但是刮擦符号的方式存在问题。

这是工作代码:

soup = BeautifulSoup(message['content'], 'lxml') 
content = (None, soup2markup(soup.find(name='body')))

def soup2markup(self, soup: Any) -> List[Any]:
    for element in soup:
        if (element.name == 'span' and element.attrs and
            ('katex-display' in element.attrs.get('class', []) or
             'katex' in element.attrs.get('class', []))):
            markup.append(element.text)
    return markup

打印markup,即显示诸如π(其katex文本为$$\pi$$等符号的刮擦文本,这给了我

π\ππ

而不只是

π

为什么会发生这种情况,我该如何解决?

<div class="message_content rendered_markdown">
<p>
<span class="katex">
<span class="katex-mathml">
<math>
<semantics>
<mrow>
<mi>π</mi>
</mrow>
<annotation encoding="application/x-tex">\pi</annotation>
</semantics>
</math>
</span>
<span aria-hidden="true" class="katex-html">
<span class="strut" style="height:0.43056em;">
</span>
<span class="strut bottom" style="height:0.43056em;vertical-align:0em;">
</span>
<span class="base">
<span class="mord mathit" style="margin-right:0.03588em;">π</span>
</span>
</span>
</span>
</p>
</div>
蛇魅

我无法直接重现您的问题-您的代码对我不起作用。但是考虑到这个范围,s

<span class="katex-mathml">
<math>
<semantics>
<mrow>
<mi>π</mi>
</mrow>
<annotation encoding="application/x-tex">\pi</annotation>
</semantics>
</math>
</span>

我们可以看到访问其text属性会显示问题:

>>> s.text
'\n\n\n\nπ\n\n\\pi\n\n\n'
>>> print(s.text.replace('\n', ''))
π\pi

因此解决方案是应用更精确的搜索条件,例如:

>>> m = s.find(name='mi')
>>> m
<mi>π</mi>
>>> m.text
'π'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用BeautifulSoup抓取网站时阅读页码

抓取网站时BeautifulSoup无法显示所有数据

当我尝试使用BeautifulSoup从网站抓取时缺少文本

使用 BeautifulSoup 或 Selenium 抓取网站时找不到数据

使用BeautifulSoup抓取特定网站

使用 python 抓取网站 - BeautifulSoup

无法使用beautifulsoup抓取日本网站

使用BeautifulSoup网站抓取IMDb页面

无法使用BeautifulSoup4抓取网站

使用BeautifulSoup抓取网站后缺少文本

使用BeautifulSoup 4.8.2从网站抓取表格

使用 BeautifulSoup 从网站抓取数据的问题

不使用beautifulsoup抓取网站数据

从网站抓取数据时 IMPORTXML 显示错误

在python 3.6中使用beautifulsoup4抓取网站以获取产品信息时

当网站阻止不抓取内容时,如何使用Python抓取?

使用BeautifulSoup抓取Google时出现SyntaxError

使用BeautifulSoup进行网页抓取时出错

使用 BeautifulSoup 抓取网页时出错

使用 beautifulsoup 抓取图像时出错

使用 BeautifulSoup 进行网页抓取时无法在 a 标签中显示文本

使用beautifulsoup抓取HTML网站ID的特定部分

使用python中的BeautifulSoup从网站抓取报告

使用Selenium和BeautifulSoup输入内容来抓取网站?

如何使用Beautifulsoup从网站上抓取产品价格?

使用 BeautifulSoup 抓取网站:TypeError: 'NoneType' 对象不可调用

使用python 2.7和beautifulsoup 4进行网站抓取

如何使用BeautifulSoup从eCom网站上抓取<li>?

需要使用xpath和beautifulsoup从网站抓取数据