关于Python 3的一些问题。
def AllMusic():
myList1 = ["bob"]
myList2 = ["dylan"]
x = myList1[0]
z = myList2[0]
y = "-->Then 10 Numbers?"
print("AllMusic")
print("http://www.allmusic.com/artist/"+x+"-"+z+"-mn"+y)
到目前为止,这是我的代码。
我想写一个打印出变量y的程序。
当你去AllMusic.com。不同的艺术家有10个独特的数字。
例如,www.allmusic.com/artist/the-beatles-mn0000754032,www.allmusic.com/artist/arcade-fire-mn0000185591。
x是艺术家的第一个单词,y是艺术家的第二个单词。一切正常,但我找不到找到这个10位数字的方法,并为我输入我的python程序的每个艺术家将它返回给我。
我发现当你去谷歌并输入例如“Arcade Fire AllMusic”时,在标题下面的第一个结果中,它会为你提供网站的网址。 www.allmusic.com/artist/arcade-fire-mn0000185591
如何将该10位数代码0000185591复制到我的python程序中并打印出来供我查看。
答案 0 :(得分:0)
我根本不会使用Google - 您可以在网站上使用搜索功能。有许多有用的工具可以帮助你在python中进行网页抓取:我建议安装BeautifulSoup。这是一个可以试验的小脚本:
import urllib
from bs4 import BeautifulSoup
def get_artist_link(artist):
base = 'http://www.allmusic.com/search/all/'
# encode spaces
query = artist.replace(' ', '%20')
url = base + query
page = urllib.urlopen(url)
soup = BeautifulSoup(page.read())
artists = soup.find_all("li", class_ = "artist")
for artist in artists:
print(artist.a.attrs['href'])
if __name__ == '__main__':
get_artist_link('the beatles')
get_artist_link('arcade fire')
对我来说,打印出来:
/artist/the-beatles-mn0000754032
/artist/arcade-fire-mn0000185591