鱼C论坛

 找回密码
 立即注册
查看: 981|回复: 2

[已解决]selenium爬取不到完整的网页源代码

[复制链接]
发表于 2020-2-14 19:23:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我是用selenium和chromedriver.exe进行爬取网易云音乐,为什么得到的网页源代码不是经过js渲染后的。
最佳答案
2020-2-15 23:18:08
  1. import time
  2. from selenium import webdriver

  3. def update():
  4.     options = webdriver.ChromeOptions()
  5.     driver = webdriver.Chrome('./chrome-win/chromedriver.exe',options=options)
  6.     driver.get('https://music.163.com/')
  7.     html=driver.find_elements_by_tag_name('html')[0]
  8.     print(html.text)
  9.     driver.switch_to.frame('contentFrame')
  10.     driver.find_elements_by_class_name('dec')[0].click()
  11.     driver.find_element_by_css_selector('#content-operation > a.u-btn2.u-btn2-2.u-btni-addply.f-fl').click()
  12.     while True:
  13.         time.sleep(1)
  14.         continue
  15.     driver.quit()
  16. if __name__ == "__main__":
  17.     update()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-14 19:24:15 | 显示全部楼层
你的代码呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 23:18:08 | 显示全部楼层    本楼为最佳答案   
  1. import time
  2. from selenium import webdriver

  3. def update():
  4.     options = webdriver.ChromeOptions()
  5.     driver = webdriver.Chrome('./chrome-win/chromedriver.exe',options=options)
  6.     driver.get('https://music.163.com/')
  7.     html=driver.find_elements_by_tag_name('html')[0]
  8.     print(html.text)
  9.     driver.switch_to.frame('contentFrame')
  10.     driver.find_elements_by_class_name('dec')[0].click()
  11.     driver.find_element_by_css_selector('#content-operation > a.u-btn2.u-btn2-2.u-btni-addply.f-fl').click()
  12.     while True:
  13.         time.sleep(1)
  14.         continue
  15.     driver.quit()
  16. if __name__ == "__main__":
  17.     update()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 20:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表