【发布时间】: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