爬虫处理iframe的方法

iframe是前端内嵌页面,访问域名与主网页不同,requests请求无法获取,技术可以的话,可以从解析js抓取iframe域名,要技术过硬啊,需要的参数很多以后再说
上代码

#!/usr/bin/python3
# -*- coding: utf-8 -*-


from selenium import webdriver

browser = webdriver.Chrome('./chromedriver')
browser.get("https://music.163.com/#/discover/playlist")


while True:
    # 切换到iframe中
    # 获取 iframe 对象
    iframe_elemnt = browser.find_element_by_id("g_iframe")
    browser.switch_to.frame(iframe_elemnt)


    elements = browser.find_elements_by_xpath('//ul[@id="m-pl-container"]/li')
    for element in elements:
        title = element.find_element_by_css_selector('.tit.f-thide.s-fc0').text
        author = element.find_element_by_css_selector('.nm.nm-icn.f-thide.s-fc3').text
        print(author,"=>",title)
    try:
        next_element = browser.find_element_by_link_text("下一页")
        next_href = next_element.get_attribute("href")
        browser.get(next_href)
    except:
        break



browser.quit()

你可能感兴趣的:(爬虫)