Python 使用转义ascii字符串正确解析html页面

Python 使用转义ascii字符串正确解析html页面,python,html,parsing,web,web-crawler,Python,Html,Parsing,Web,Web Crawler,我目前正在用Python开发一个爬虫程序,它已经在爬虫一个流派页面来获取所有乐队和专辑,然后爬虫这些链接来获取特定歌曲的链接,最后解析歌词并将其放入数据库,这样它可以帮助我分析歌词内容 我让我的爬虫来完成所有这些步骤,但是当我用urllib和beautifulsoup解析抒情页面中的html时,我得到了奇怪的内容。我对此进行了调查,似乎有一个脚本可以阻止人们爬行?当查看html源代码时,歌词如下所示。我不知道该称之为什么,所以很遗憾,我无法独自做进一步的研究,而不知道该寻找什么 <div

我目前正在用Python开发一个爬虫程序,它已经在爬虫一个流派页面来获取所有乐队和专辑,然后爬虫这些链接来获取特定歌曲的链接,最后解析歌词并将其放入数据库,这样它可以帮助我分析歌词内容

我让我的爬虫来完成所有这些步骤,但是当我用urllib和beautifulsoup解析抒情页面中的html时,我得到了奇怪的内容。我对此进行了调查,似乎有一个脚本可以阻止人们爬行?当查看html源代码时,歌词如下所示。我不知道该称之为什么,所以很遗憾,我无法独自做进一步的研究,而不知道该寻找什么

<div class='lyricbox'>&#73;&#116;&#32;&#119;&#97;&#115;&#32;&#119;&#104;&#101;&#110;&#32;&#73;&#32;&#114;&#101;&#97;&#108;&#105;&#122;&#101;&#100;<br />&#116;&#104;&#97;&#116;&#32;&#108;&#105;&#102;&#101;&#32;&#104;&#97;&#115;&#32;&#110;&#111;&#32;&#109;&#101;&#97;&#110;&#105;&#110;&#103;<br />&#110;&#111;&#32;&#112;&#117;&#114;&#112;&#111;&#115;&#101;&#44;&#32;&#110;&#111;&#32;&#113;&#117;&#97;&#114;&#114;&#121;<br />&#46;&#46;&#46;&#110;&#111;&#32;&#97;&#110;&#115;&#119;&#101;&#114;&#101;&#115;&#46;&#46;&#46;<br /><br />&#65;&#110;&#100;&#32;&#97;&#108;&#108;&#32;&#116;&#104;&#101;&#32;&#100;&#114;&#101;&#97;&#114;&#121;&#32;&#110;&#105;&#103;&#104;&#116;<br />&#116;&#104;&#97;&#116;&#32;&#104;&#97;&#100;&#32;&#98;&#101;&#102;&#97;&#108;&#108;&#101;&#110;&#32;&#97;&#99;&#114;&#111;&#115;&#115;<br />&#116;&#104;&#101;&#32;&#108;&#97;&#110;&#100;<br />&#73;&#32;&#115;&#108;&#105;&#112;&#112;&#101;&#100;&#32;&#105;&#110;&#116;&#111;&#32;&#97;&#32;&#114;&#101;&#118;&#101;&#114;&#121;<br />&#97;&#32;&#119;&#101;&#98;&#32;&#111;&#102;&#32;&#104;&#117;&#109;&#97;&#110;&#32;&#104;&#97;&#110;&#100;<br /><br />&#89;&#111;&#117;&#32;&#108;&#111;&#110;&#103;&#101;&#100;&#32;&#116;&#111;&#32;&#115;&#111;&#97;&#114;&#32;&#117;&#112;&#32;&#104;&#105;&#103;&#104;<br />&#116;&#111;&#32;&#99;&#97;&#114;&#101;&#115;&#115;&#32;&#116;&#104;&#101;&#32;&#115;&#105;&#108;&#107;&#121;&#32;&#119;&#105;&#110;&#100;&#115;<br />&#116;&#111;&#32;&#101;&#109;&#98;&#114;&#97;&#99;&#101;&#32;&#97;&#110;&#100;&#32;&#107;&#105;&#115;&#115;&#32;&#97;&#115;&#32;&#108;&#111;&#118;&#101;&#114;&#115;<br />&#46;&#46;&#46;&#116;&#104;&#101;&#32;&#101;&#116;&#104;&#101;&#114;&#46;&#46;&#46;<br /><br 

It was when I realized
和#116hat life has no meaning
和#110o purpose, no quarry
和#46..no answeres...

&65nd all the dreary night
和#116hat had befallen across
和#116he land
和#73 slipped into a revery
和#97 web of human hand

&89ou longed to soar up high
和#116o caress the silky winds
和#116o embrace and kiss as lovers
和#46..the ether...
这些是HTML编码的字符:


你只需要解码它们。可能有一个现有的工具可以用来解码它们。

你应该发布我们可以帮助调试的代码,我猜你没有使用正确的编码方案<代码>导入请求
适用于我:

>>> import requests
>>> import bs4
>>> url = "http://lyrics.wikia.com/wiki/Agalloch:The_Wilderness"
>>> req = requests.get(url)
>>> soup = bs4.BeautifulSoup(req.text, "html.parser")
>>> lyrics = soup.find("div", {"class":"lyricbox"})
>>> lyrics.get_text().rstrip()
这将返回:

"It was when [... ] the cosmos...Forevermore..."

所以,事实证明这些是ascii字符的整数值。在您的脚本中,您可以这样做以恢复可打印的ascii

>>> a = '&#73;&#116;&#32;&#119;&#97;&#115;&#32;&#119;&#104;&#101;&#110;&#32;&#73;&#32;&#114;&#101;&#97;&#108;&#105;&#122;&#101;&#100;'
>>> ''.join(map(chr,map(int,a.replace('&#','').split(';')[:-1])))                        
'It was when I realized'

希望这有帮助

这些是转义HTML实体,例如
&用于
。和
&具有十进制和十六进制等效表示形式。你的课文充满了小数。这是你怎么做的

import html
s = "<div class='lyricbox'>&#73;&#116;&#32;&#119;&#97;&#115;&#32;&#119;&#104;&#101;&#110;&#32;&#73;&#32;&#114;&#101;&#97;&#108;&#105;&#122;&#101;&#100;<br />&#116;&#104;&#97;&#116;&#32;&#108;&#105;&#102;&#101;&#32;&#104;&#97;&#115;&#32;&#110;&#111;&#32;&#109;&#101;&#97;&#110;&#105;&#110;&#103;<br />&#110;&#111;&#32;&#112;&#117;&#114;&#112;&#111;&#115;&#101;&#44;&#32;&#110;&#111;&#32;&#113;&#117;&#97;&#114;&#114;&#121;<br />&#46;&#46;&#46;&#110;&#111;&#32;&#97;&#110;&#115;&#119;&#101;&#114;&#101;&#115;&#46;&#46;&#46;<br /><br />&#65;&#110;&#100;&#32;&#97;&#108;&#108;&#32;&#116;&#104;&#101;&#32;&#100;&#114;&#101;&#97;&#114;&#121;&#32;&#110;&#105;&#103;&#104;&#116;<br />&#116;&#104;&#97;&#116;&#32;&#104;&#97;&#100;&#32;&#98;&#101;&#102;&#97;&#108;&#108;&#101;&#110;&#32;&#97;&#99;&#114;&#111;&#115;&#115;<br />&#116;&#104;&#101;&#32;&#108;&#97;&#110;&#100;<br />&#73;&#32;&#115;&#108;&#105;&#112;&#112;&#101;&#100;&#32;&#105;&#110;&#116;&#111;&#32;&#97;&#32;&#114;&#101;&#118;&#101;&#114;&#121;<br />&#97;&#32;&#119;&#101;&#98;&#32;&#111;&#102;&#32;&#104;&#117;&#109;&#97;&#110;&#32;&#104;&#97;&#110;&#100;<br /><br />&#89;&#111;&#117;&#32;&#108;&#111;&#110;&#103;&#101;&#100;&#32;&#116;&#111;&#32;&#115;&#111;&#97;&#114;&#32;&#117;&#112;&#32;&#104;&#105;&#103;&#104;<br />&#116;&#111;&#32;&#99;&#97;&#114;&#101;&#115;&#115;&#32;&#116;&#104;&#101;&#32;&#115;&#105;&#108;&#107;&#121;&#32;&#119;&#105;&#110;&#100;&#115;<br />&#116;&#111;&#32;&#101;&#109;&#98;&#114;&#97;&#99;&#101;&#32;&#97;&#110;&#100;&#32;&#107;&#105;&#115;&#115;&#32;&#97;&#115;&#32;&#108;&#111;&#118;&#101;&#114;&#115;<br />&#46;&#46;&#46;&#116;&#104;&#101;&#32;&#101;&#116;&#104;&#101;&#114;&#46;&#46;&#46;<br /><br>"
html.unescape(s)
"<div class='lyricbox'>It was when I realized<br />that life has no meaning<br />no purpose, no quarry<br />...no answeres...<br /><br />And all the dreary night<br />that had befallen across<br />the land<br />I slipped into a revery<br />a web of human hand<br /><br />You longed to soar up high<br />to caress the silky winds<br />to embrace and kiss as lovers<br />...the ether...<br /><br>"
导入html
s="It was when I realized
和#116hat life has no meaning
和#110o purpose, no quarry
和#46..no answeres...

&65nd all the dreary night
和#116hat had befallen across
和#116he land
和#73 slipped into a revery
和#97 web of human hand

&89ou longed to soar up high
和#116o caress the silky winds
和#116o embrace and kiss as lovers
和#46..the ether...

“ html.unescape(s) “当我意识到生活没有意义时,没有采石场,
没有回答,
还有那片土地上所有沉闷的夜晚,
我陷入了一个梦乡,
一张人类的手网,

你渴望高飞,
爱抚柔滑的风,
像情人一样拥抱和亲吻,
以太……

一个好的解析器会处理这个问题,即使是最简单的
HTMLParser
也会处理这个问题。

非常感谢,这很有效!我使用了几乎相同的方法,除了我使用urllib获取“req”,lxml作为解析器而不是html.parser,并且我没有使用get_text().rstrip().我想我不会编辑我的帖子来显示我的代码是什么,因为这已经得到了回答(或者我应该继续吗?)你很好,问题仍然是人们将来会得到像你一样的回答,这个帖子还有一些关于编码/解码的好答案谢谢,我不明白你为什么这么做,但我会阅读ascii字符的理论,这样我以后会读。我基本上是把原始字符串和所有的