可以用 driver.switch_frame()函数切换框架就可以得到你想要的源代码了。
请看示例,这是我以前写的网易云音乐爬虫:# 此脚本需要配合谷歌浏览器与谷歌驱动
# 作者:那宏岩
import urllib.request
import re
from selenium import webdriver
from selenium.webdriver import ChromeOptions
def search(music=None,singer=None):
if not music and not singer:
raise ValueError('参数错误!!')
if not music:
name = singer
else:
name = music
# 创建参数设置对象.
CLOUD_CHROME_OPTION = ChromeOptions()
CLOUD_CHROME_OPTION.binary_location = '.\\plug-in\\Google\\Chrome\\Application\\chrome.exe'
# chrome_opt.add_argument('--headless') # 无界面化.
# chrome_opt.add_argument('--disable-gpu') # 配合上面的无界面化.
driver = webdriver.Chrome(options = CLOUD_CHROME_OPTION,executable_path=r'chromedriver.exe')
driver.get('https://music.163.com/#/search/m/?s=' + name)
driver.switch_to.frame('g_iframe')
music_list = re.findall(r'<a href="/song\?id=(\d*)"><b title="(.*?)">',driver.page_source)
singer_list = re.findall(r'<a href="/artist\?id=\d*">(.*?)</a>',driver.page_source)
# driver.quit()
music_num = len(music_list)
download_list = list()
if singer:
for i in range(music_num):
if singer in singer_list[i]:
download_list.append(music_list[i]+(singer_list[i],))
else:
for i in range(music_num):
download_list.append(music_list[i]+(singer_list[i],))
print(download_list)
down_load_sign = input(r'开始下载吗?[Y\N]:')
if down_load_sign == 'Y':
for each_music in download_list:
print(f'{each_music[1]}-{each_music[2]}.mp3正在下载!')
urllib.request.urlretrieve(f'http://music.163.com/song/media/outer/url?id={each_music[0]}.mp3',f'C:\\CLOUD Artification Intelligence\\clouddata\\Sound\\{each_music[1]}-{each_music[2]}.mp3')
print(f'{each_music[1]}-{each_music[2]}.mp3下载完成!')
if __name__ == '__main__':
while True:
# music_name = input('请输入歌曲名:')
# singer_name = input('请输入歌手名:')
music_name = '大鱼'
singer_name = ''
search(music_name,singer_name)
|