【问题标题】:Scraping returning clipped content (Python)抓取返回的剪辑内容(Python)
【发布时间】:2021-10-28 12:48:58
【问题描述】:

我有一组存储在列表中的 URL,我想编写一个脚本来收集 Genius 站点歌词并将它们存储在一个 txt 文件中。

我已经制作了这个脚本,但是由于某种原因返回的内容不完整。

代码如下:

import requests
import re
import pandas as pd
from bs4 import BeautifulSoup
from time import time

urls = ['https://genius.com/The-Stooges-1969-lyrics','https://genius.com/The-Stooges-1970-lyrics',
        'https://genius.com/The-Rolling-Stones-19th-Nervous-Breakdown-lyrics','https://genius.com/Lil-Wayne-3-Peat-lyrics',
        'https://genius.com/RunDMC-30-Days-lyrics','https://genius.com/Bob-marley-and-the-wailers-four-hundred-years-lyrics',
        'https://genius.com/The-Clash-48-Hours-lyrics']

start = time()

for u in urls:
    soup = BeautifulSoup(requests.get(u).content, 'lxml')
    for tag in soup.select('div[class^="Lyrics__Container"], .song_body-lyrics p'):
        lyrics = tag.get_text(strip=True, separator='\n')
        if lyrics:
            with open("PATH\\"+str(urls.index(u))+".txt", 'w') as f: 
                f.write(lyrics)      

print(f'Time taken: {time() - start}')

例如,在 url 上查看歌曲的歌词:https://genius.com/Rundmc-30-days-lyrics

现在看看得到的回报:

"[DMC]如果你需要假期,我们可以飞遍世界 你会知道我永远不会看另一个女孩我是一个专一的人,我的想法 已设置 你是 80 年代的女士,我会得到 [Both] 如果 你发现你不喜欢我的方式好吧,你可以在 30 天内送我回去”

不知何故,我可以访问歌词,但似乎缺少使脚本健壮的东西,因为它在某些情况下会删减内容。

有人知道我可能错了吗?

【问题讨论】:

  • 没有可复制的代码很难说。您是否尝试过在带有剪辑歌词的响应上放置调试断点并调查 html?或者如果您没有准备好调试器,只需将它们保存到文件中。看起来天才网站正在使用一些反机器人保护 - 可能是它没有为您提供完整的内容,因为它怀疑您是机器人。
  • 嗨,@Granitosaurus。上面的代码就是我制作的所有脚本。如果你觉得舒服,你可以复制它。我已经检查了汤的内容,也看到了歌词。我不知道是不是我的循环中的某些东西可能会生成此错误,或者是所选标签 (div) 与内容不匹配。

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

我真的不明白它为什么会这样做,但可能只是网站有时呈现不同。我做了一些调整,到目前为止还没有看到这个问题。可能是它解析文本的方式,然后是你写入文件的方式,所以我调整了 for 循环中的一些缩进,以了解它如何连接字符串:

import requests
import re
import pandas as pd
from bs4 import BeautifulSoup
from time import time

urls = ['https://genius.com/The-Stooges-1969-lyrics','https://genius.com/The-Stooges-1970-lyrics',
        'https://genius.com/The-Rolling-Stones-19th-Nervous-Breakdown-lyrics','https://genius.com/Lil-Wayne-3-Peat-lyrics',
        'https://genius.com/RunDMC-30-Days-lyrics','https://genius.com/Bob-marley-and-the-wailers-four-hundred-years-lyrics',
        'https://genius.com/The-Clash-48-Hours-lyrics']


start = time()
headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Mobile Safari/537.36'}
for u in urls:
    response = requests.get(u, headers=headers)
    #print(response)
    soup = BeautifulSoup(response.text, 'lxml')
    
    lyrics = ''
    for tag in soup.find_all("div", {"class":re.compile(r'^Lyrics__Container')}):
        lyrics += tag.get_text(strip=True, separator='\n') + '\n'
    if lyrics:
        with open("D:/test/lyrics/"+str(urls.index(u))+".txt", 'w') as f: 
            f.write(lyrics)  
        #print(lyrics)

print(f'Time taken: {time() - start}')

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 2019-04-27
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2020-04-24
    • 2020-02-17
    • 2019-06-07
    相关资源
    最近更新 更多