正确解析带有转义 ascii 字符串的 html 页面

Proper parsing of a html-page with escaped ascii-strings

我目前正在 Python 中开发 抓取工具 ,它已经在 [=] 上抓取类型页面12=] 获取所有乐队和专辑,然后爬取这些链接以获取特定歌曲的链接,最后解析歌词并将其放入数据库中,以便帮助我分析歌词内容。

我让爬虫完成所有这些步骤,但是当我使用 urllib[=39 从歌词页面解析 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 

使用 google chrome 开发者工具进行调查时,歌词是可读的。

示例页面是:http://lyrics.wikia.com/wiki/Agalloch:The_Wilderness

长话短说: 这是什么?它从何而来?我如何找到解决方法? (请记住,我想用大约 20000 页来做到这一点,所以最好它必须很快 and/or 可迭代

提前致谢!

这些是 HTML 个编码字符:http://www.ascii.cl/htmlcodes.htm

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

你应该 post 代码,我们可以帮助调试,我猜你没有使用正确的编码方案。 Import requests 适合我:

>>> 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()

这将 return:

"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 条目,例如 &amp; 代表 &。并且 &amp; 具有十进制和十六进制等效表示。您的文字充满了小数点。这是您的操作方法。

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>"

一个好的解析器会处理这个,即使是极简的 HTMLParser 也会处理这个。