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