selenium page_source获得的数据跟F12查看框架里面的不一样!!
我查看源码框架里面是这样的字典形式:
但是我用selenium获得的page_source确是这样的,多了一些html标签:
{:10_306:}{:10_324:}
审核元素可能是其他文件加载的,所以不一样 Twilight6 发表于 2020-7-24 09:55
审核元素可能是其他文件加载的,所以不一样
那有没有什么办法可以让我获得的界面源码是f12查看到的那种字典呢{:10_324:} 这个问题我还真的没想过 可以用 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:
download_list.append(music_list+(singer_list,))
else:
for i in range(music_num):
download_list.append(music_list+(singer_list,))
print(download_list)
down_load_sign = input(r'开始下载吗?:')
if down_load_sign == 'Y':
for each_music in download_list:
print(f'{each_music}-{each_music}.mp3正在下载!')
urllib.request.urlretrieve(f'http://music.163.com/song/media/outer/url?id={each_music}.mp3',f'C:\\CLOUD Artification Intelligence\\clouddata\\Sound\\{each_music}-{each_music}.mp3')
print(f'{each_music}-{each_music}.mp3下载完成!')
if __name__ == '__main__':
while True:
# music_name = input('请输入歌曲名:')
# singer_name = input('请输入歌手名:')
music_name = '大鱼'
singer_name = ''
search(music_name,singer_name)
可以用 selenium 模块的 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:
download_list.append(music_list+(singer_list,))
else:
for i in range(music_num):
download_list.append(music_list+(singer_list,))
print(download_list)
down_load_sign = input(r'开始下载吗?:')
if down_load_sign == 'Y':
for each_music in download_list:
print(f'{each_music}-{each_music}.mp3正在下载!')
urllib.request.urlretrieve(f'http://music.163.com/song/media/outer/url?id={each_music}.mp3',f'C:\\CLOUD Artification Intelligence\\clouddata\\Sound\\{each_music}-{each_music}.mp3')
print(f'{each_music}-{each_music}.mp3下载完成!')
if __name__ == '__main__':
while True:
# music_name = input('请输入歌曲名:')
# singer_name = input('请输入歌手名:')
music_name = '大鱼'
singer_name = ''
search(music_name,singer_name)
页:
[1]