深海的大菠萝 发表于 2020-10-24 20:01:46

selenium库怎么开启自动加载鸭,求大神指教

# 教学系统的浏览器设置方法
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from bs4 import BeautifulSoup # 调用BeautifulSoup库
import time
chrome_options = Options() # 实例化Option对象
#浏览器不提供可视化界面,Linux系统一定要加这个要不然启动报错
chrome_options.add_argument('--headless') # 对浏览器的设置
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(options=chrome_options)

driver.get('https://y.qq.com/n/yqq/song/000xdZuV2LcQ19.html') # 访问页面
time.sleep(2)

button = driver.find_element_by_class_name('js_get_more_hot') # 根据类名找到【点击加载更多】
button.click() # 点击
time.sleep(2) # 等待两秒

pageSource = driver.page_source # 获取Elements中渲染完成的网页源代码
soup = BeautifulSoup(pageSource,'html.parser')# 使用bs解析网页
comments = soup.find('ul',class_='js_hot_list').find_all('li',class_='js_cmt_li') # 使用bs提取元素
print(len(comments)) # 打印comments的数量

for comment in comments: # 循环
    sweet = comment.find('p') # 提取评论
    print ('评论:%s\n ---\n'%sweet.text) # 打印评论
driver.close() # 关闭浏览器 # 关闭浏览器
页: [1]
查看完整版本: selenium库怎么开启自动加载鸭,求大神指教