【问题标题】:Retrieving a subset of href's from findall() in BeautifulSoup从 BeautifulSoup 中的 findall() 检索 href 的子集
【发布时间】:2017-09-01 21:08:03
【问题描述】:

我的目标是编写一个 python 脚本,将艺术家的姓名作为字符串输入,然后将其附加到天才搜索查询的基本 URL。然后从返回的网页链接中检索所有歌词(即这个问题的所需子集也将包含该子集中每个链接中的艺术家姓名。)。我现在处于初始阶段,只是能够从网页中检索所有链接,包括我没有的链接不想在我的子集中。我试图找到一个简单的解决方案,但不断失败。

import requests
# The Requests library.

from bs4 import BeautifulSoup
from lxml import html

user_input = input("Enter Artist Name = ").replace(" ","+")
base_url = "https://genius.com/search?q="+user_input

header = {'User-Agent':''}
response = requests.get(base_url, headers=header)

soup = BeautifulSoup(response.content, "lxml")

for link in soup.find_all('a',href=True):
        print (link['href'])

这将返回这个完整列表,而我只需要以歌词和艺术家姓名结尾的列表(例如 Drake)。这些将是我应该能够从中检索歌词的链接。

https://genius.com/
/signup
/login
https://www.facebook.com/geniusdotcom/
https://twitter.com/Genius
https://www.instagram.com/genius/
https://www.youtube.com/user/RapGeniusVideo
https://genius.com/new
https://genius.com/Drake-hotline-bling-lyrics
https://genius.com/Drake-one-dance-lyrics
https://genius.com/Drake-hold-on-were-going-home-lyrics
https://genius.com/Drake-know-yourself-lyrics
https://genius.com/Drake-back-to-back-lyrics
https://genius.com/Drake-all-me-lyrics
https://genius.com/Drake-0-to-100-the-catch-up-lyrics
https://genius.com/Drake-started-from-the-bottom-lyrics
https://genius.com/Drake-from-time-lyrics
https://genius.com/Drake-the-motto-lyrics
/search?page=2&q=drake
/search?page=3&q=drake
/search?page=4&q=drake
/search?page=5&q=drake
/search?page=6&q=drake
/search?page=7&q=drake
/search?page=8&q=drake
/search?page=9&q=drake
/search?page=672&q=drake
/search?page=673&q=drake
/search?page=2&q=drake
/embed_guide
/verified-artists
/contributor_guidelines
/about
/static/press
mailto:brands@genius.com
https://eventspace.genius.com/
/static/privacy_policy
/jobs
/developers
/static/terms
/static/copyright
/feedback/new
https://genius.com/Genius-how-genius-works-annotated
https://genius.com/Genius-how-genius-works-annotated

我的下一步将是使用 selenium 来模拟滚动,在 Genius.com 的情况下,它提供了整个搜索结果集。任何建议或资源将不胜感激。我还想了解一些关于我希望继续使用此解决方案的方式。我们可以让它更通用吗?

附:我可能没有清楚地解释我的问题,但我已经尽力了。此外,也欢迎任何歧义。我对抓取、python 和编程也是新手,只是想确保我走的是正确的道路。

【问题讨论】:

    标签: python python-2.7 web-scraping beautifulsoup lxml


    【解决方案1】:

    使用正则表达式模块只匹配你想要的链接。

    import requests
    # The Requests library.
    
    from bs4 import BeautifulSoup
    from lxml import html
    from re import compile
    
    user_input = input("Enter Artist Name = ").replace(" ","+")
    base_url = "https://genius.com/search?q="+user_input
    
    header = {'User-Agent':''}
    response = requests.get(base_url, headers=header)
    
    soup = BeautifulSoup(response.content, "lxml")
    
    pattern = re.compile("[\S]+-lyrics$")
    
    for link in soup.find_all('a',href=True):
        if pattern.match(link['href']):
            print (link['href'])
    

    输出:

    https://genius.com/Drake-hotline-bling-lyrics
    https://genius.com/Drake-one-dance-lyrics
    https://genius.com/Drake-hold-on-were-going-home-lyrics
    https://genius.com/Drake-know-yourself-lyrics
    https://genius.com/Drake-back-to-back-lyrics
    https://genius.com/Drake-all-me-lyrics
    https://genius.com/Drake-0-to-100-the-catch-up-lyrics
    https://genius.com/Drake-started-from-the-bottom-lyrics
    https://genius.com/Drake-from-time-lyrics
    https://genius.com/Drake-the-motto-lyrics
    

    这只是查看您的链接是否与以-lyrics 结尾的模式匹配。您也可以使用类似的逻辑来过滤使用user_input 变量。

    希望这会有所帮助。

    【讨论】:

    • 您能否就从这些链接中检索歌词提出一些建议?问题在于,genius.com 中的歌词被大量注释,并且内容(文本中的歌词)分散在很多行中。我想过循环通过 xpath。
    • 我不能提供代码给你,因为我工作有点忙。但我会采用以下方式: 1. 将歌词网页汤化 2. find() out <div class="lyrics"> 3. find_all()<a href> 标签。 4. 使用.string 为上面步骤 3 中的每个标签累积文本。希望您在抓取之前Inspect Element 每个网页。 :)
    猜你喜欢
    • 2015-02-27
    • 1970-01-01
    • 2012-05-18
    • 2017-01-21
    • 1970-01-01
    • 2012-09-02
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多