我目前正在用Python开发一个爬虫,它已经在http://lyrics.wikia.com/上爬行一个流派页面。获取所有乐队和专辑,然后抓取这些链接以获取特定歌曲的链接,最后解析歌词并将它们放入数据库中,这样它可以帮助我分析歌词内容。
我让我的爬虫完成所有这些步骤,但是当我使用 urllib 和 beautifulsoup 解析歌词页面的 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 个页面执行此操作,因此最好它必须快速和/或可迭代
提前致谢!
最佳答案
您应该发布我们可以帮助调试的代码,我猜您没有使用正确的编码方案。 导入请求
对我有用:
>>> 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..."
关于python - 使用转义的 ascii 字符串正确解析 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169347/