admin管理员组文章数量:1619290
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdrivermon.by import By # BY:定位策略
Options = webdriver.ChromeOptions() # 创建谷歌浏览器设置对象
Options.add_experimental_option('detach', True)
# 创建谷歌浏览器对象
Chrome_Object = webdriver.Chrome(service=Service(executable_path='.\chromedriver.exe'), options=Options)
URL = 'https://101.qq/#/hero'
# 得到的数据全保存在Chrome_Object对象中,后续的操作基本上都在这个对象上
Chrome_Object.get(url=URL)
# 有些时候会拿不到数据的原因是selenium判定网页加载完成是等标签页前的刷新图标是否消失
# 为了解决页面没加载完selenium就继续向下执行代码,需要添加等待
# 方式三种:休眠,隐形等待,显示等待
time.sleep(3)
Chrome_Object.maximize_window()
# Chrome_Object.set_window_size(800, 800) 设置窗口大小
# print(Chrome_Object.page_source) 第一种打印数据的方式,但不推荐使用,打印出来是字符串,可以用bs4模块操作
#
# Chrome_Object.get(url='http://taobao')
# time.sleep(2)
# Chrome_Object.back()
# time.sleep(2)
# Chrome_Object.forward()
hero_all_information_list = Chrome_Object.find_elements(
By.CSS_SELECTOR, '#app > div > div.app-main > div > div.app-main-container.infomation-overview > ul > li')
# print(hero_all_information_list)
for i in hero_all_information_list:
hero_name = i.find_element(By.CSS_SELECTOR, 'li > div > p').text
hero_img_href = i.find_element(By.CSS_SELECTOR, 'li > div > div > img').get_attribute('src')
print(hero_name, hero_img_href)
# Chrome_Object.close() # 若网页有多个页面只关闭当前页面
# Chrome_Object.quit() # 关闭所有页面
# nth-of-type:找某个标签的第几个
# nth-child:找同级标签的第几个
版权声明:本文标题:selenium爬取LOL 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728794867a1174071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论