正确解析带有转义 ascii 字符串的 html 页面
Proper parsing of a html-page with escaped ascii-strings
我目前正在 Python 中开发 抓取工具 ,它已经在 [=] 上抓取类型页面12=] 获取所有乐队和专辑,然后爬取这些链接以获取特定歌曲的链接,最后解析歌词并将其放入数据库中,以便帮助我分析歌词内容。
我让爬虫完成所有这些步骤,但是当我使用 urllib 和 [=39 从歌词页面解析 html 时=] 我收到奇怪的内容。我对此进行了调查,似乎有一个脚本可以阻止人们爬行?查看html-源代码时,歌词如下所示。我不知道该怎么称呼它,很遗憾我无法在不知道要寻找什么的情况下自己做进一步的研究。
<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
使用 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 = 'It was when I realized'
>>> ''.join(map(chr,map(int,a.replace('&#','').split(';')[:-1])))
'It was when I realized'
希望对您有所帮助!
这些是转义的 HTML 条目,例如 &
代表 &
。并且 &
具有十进制和十六进制等效表示。您的文字充满了小数点。这是您的操作方法。
import html
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.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
也会处理这个。
我目前正在 Python 中开发 抓取工具 ,它已经在 [=] 上抓取类型页面12=] 获取所有乐队和专辑,然后爬取这些链接以获取特定歌曲的链接,最后解析歌词并将其放入数据库中,以便帮助我分析歌词内容。
我让爬虫完成所有这些步骤,但是当我使用 urllib 和 [=39 从歌词页面解析 html 时=] 我收到奇怪的内容。我对此进行了调查,似乎有一个脚本可以阻止人们爬行?查看html-源代码时,歌词如下所示。我不知道该怎么称呼它,很遗憾我无法在不知道要寻找什么的情况下自己做进一步的研究。
<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
使用 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 = 'It was when I realized'
>>> ''.join(map(chr,map(int,a.replace('&#','').split(';')[:-1])))
'It was when I realized'
希望对您有所帮助!
这些是转义的 HTML 条目,例如 &
代表 &
。并且 &
具有十进制和十六进制等效表示。您的文字充满了小数点。这是您的操作方法。
import html
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.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
也会处理这个。