响应。文本返回字节和怪异符号

发布于 2025-02-01 18:12:37 字数 1165 浏览 0 评论 0原文

我正在尝试刮擦网站 url ='https://spar.pl/gazetka/spar/', 并尝试使用控制台或VS代码:

requests.get(url).text

返回我未知的字节和其他符号。它如何开始的示例:

`\x139��HM�!�\x11:|�{R�����-z\x08~+�O\x00�\x14Ʌ<λ����dk4 \x00R�(�\x0bPɲ.Ģ��Eu��n���/_M�L�\x14�X�ܪt-������\x07�||��\x07��\x00��\x0c\x08:�i�T�ݿ\x7f?\x08�\'\x1c��I!�\x14\x03\x15R�\\�n\x15b\'���\x14�N���+�(��������\u05c9\x04HB\x08�hou\x0c�~/~�v���9ԯ?g��"�sZ\x13# ��j\r*�j)�K|�\x19NZ��s�\r��N�q����=x\x05�\x0b��2�0\x05���V�\x7f��������\x02�}���\x18Pl�aU����\'\x1d˷N�a`�\x0f���:���w&\x0b�\t��5���Y-fВ\x14\x10�?��\x17\x0f\r\x11�s��%n`

奇怪的是,当我在jupyter笔记本中尝试相同的代码时,我返回了普通文本,示例

'<!DOCTYPE html>\n<html lang="pl-PL">\n\n\t<head>\t\n\t\t<title>SPAR |  SPAR &#8211; świeżość i wybór gwarantowane</title>\n\t\t\n\t\t<meta charset=\'UTF-8\'>\n\t\t<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=n

,还有一个几乎相同的https://spar.par.pal.ply.pl/gazetka/ EUROSPAR/,这两种方法都返回干净的文本。

我已经尝试解码结果,或使用Response.content,但我觉得它没有帮助。我真的很好奇为什么问题出现了,以及如何解决问题。

I'm trying to scrape a site
url = 'https://spar.pl/gazetka/spar/',
and trying in either console or VS Code:

requests.get(url).text

returns a jumble of bytes and other symbols unknown to me. The example of how it starts:

`\x139��HM�!�\x11:|�{R�����-z\x08~+�O\x00�\x14Ʌ<λ����dk4 \x00R�(�\x0bPɲ.Ģ��Eu��n���/_M�L�\x14�X�ܪt-������\x07�||��\x07��\x00��\x0c\x08:�i�T�ݿ\x7f?\x08�\'\x1c��I!�\x14\x03\x15R�\\�n\x15b\'���\x14�N���+�(��������\u05c9\x04HB\x08�hou\x0c�~/~�v���9ԯ?g��"�sZ\x13# ��j\r*�j)�K|�\x19NZ��s�\r��N�q����=x\x05�\x0b��2�0\x05���V�\x7f��������\x02�}���\x18Pl�aU����\'\x1d˷N�a`�\x0f���:���w&\x0b�\t��5���Y-fВ\x14\x10�?��\x17\x0f\r\x11�s��%n`

Oddly enough, when I try the same code in Jupyter Notebook, I'm returned with normal text, example

'<!DOCTYPE html>\n<html lang="pl-PL">\n\n\t<head>\t\n\t\t<title>SPAR |  SPAR – świeżość i wybór gwarantowane</title>\n\t\t\n\t\t<meta charset=\'UTF-8\'>\n\t\t<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=n

What's more, a site that's nearly identical https://spar.pl/gazetka/eurospar/, both methods return clean text.

I've tried decoding the result, or using response.content, but I couldn't find it helpful. I'm really curious why the problem occurs at all, and how to possibly solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2025-02-08 18:12:37

尝试

response.encoding = 'utf-8'
text = response.text

try

response.encoding = 'utf-8'
text = response.text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文