from selenium.webdriver import Chrome
from bs4 import BeautifulSoup
# 1. 创建浏览器对象
b = Chrome()
# 2. 在浏览器中输入网址
b.get('https://movie.douban.com/top250')
# 3. 获取网页内容(当前浏览器显示的是哪个页面,获取就是那个页面的内容)
# print(b.page_source)
soup = BeautifulSoup(b.page_source, 'lxml')
imgs = soup.select('.pic>a>img')
for x in imgs:
print(x.attrs['alt'])
# 4. 关闭浏览器
b.close()
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
# 1.创建浏览器对象
b = Chrome()
# 2. 输入网址(打开网页)
b.get('https://www.jd.com/')
# 3. 获取标签
# 浏览器.find_element_by_xxxx()
input = b.find_element_by_id('key')
btn = b.find_element_by_css_selector('.form>.button')
# 4. 操作标签
# 1)输入框输入内容
# a.输入普通文字内容
input.send_keys('笔记本电脑')
# b.按回车
# input.send_keys(Keys.ENTER)
# 2)点击按钮
btn.click()
time.sleep(1)
print(b.page_source)
网站信息获取
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup
b = Chrome()
b.get('https://www.51job.com/')
search_input = b.find_element_by_id('kwdselectid')
search_input.send_keys('数据分析')
search_input.send_keys(Keys.ENTER)
for _ in range(10):
time.sleep(1)
soup = BeautifulSoup(b.page_source, 'lxml')
jobs_div = soup.select('.j_joblist>div')
for job in jobs_div:
name = job.select_one('.t>span').text
money = job.select_one('.sal').text
print(name, money)
# 翻页
# next = b.find_element_by_class_name('next')
# next.click()
next = b.find_element_by_css_selector('.e_icons.i_next')
next.click()
基础配置
from selenium.webdriver import Chrome, ChromeOptions
# 1.创建设置对象
options = ChromeOptions()
# 2.添加配置
# 1)取消测试环境
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 2)取消图片加载
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
b = Chrome(options=options)
b.get('https://www.jd.com')
商品信息获取
from selenium.webdriver import Chrome, ChromeOptions
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
import time
options = ChromeOptions()
# 2.添加配置
# 1)取消测试环境
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 2).取消图片加载
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
b = Chrome(options=options)
b.get('https://www.jd.com/')
# 3.获取标签
# 浏览器.find_element_by_xxxx()
# 获取输入框
search = b.find_element_by_id('key')
# 4.操作标签
# 1)输入框输入内容
search.send_keys('零食')
# b.按回车
search.send_keys(Keys.ENTER)
all_name = []
# 循环次数决定页数
for i in range(2):
# 滚动
# 网页滚动的理论:执行js代码:window.scrollTo(x坐标,y坐标)
height = 500
while True:
time.sleep(1)
b.execute_script(f"window.scrollTo(0,{height})")
height += 500
if height > 8000:
break
# 爬取当前页面内容
soup = BeautifulSoup(b.page_source, 'lxml')
all_goods_li = soup.select('#J_goodsList li')
# 循环解析出需要的内容
for li in all_goods_li:
name = li.select_one('.p-name.p-name-type-2 em').text
all_name.append(name)
# 换页代码操作
time.sleep(5)
next1 = b.find_element_by_css_selector('.p-num>.pn-next')
next1.click()
print(all_name)
print(len(all_name))
1. 隐式等待
设置隐式等待时间,可以让浏览器在获取标签的时候,如果标签因为网络或者系统的原因导致标签没有加载出来而取不到的时候,
不会直接报错,而是在指定的时间范围内,不断获取标签,直到获取到标签或者超时为止。
一个浏览器对象只需要设置一次隐式等待时间
使用方法:
浏览器对象.implicitly_wait(超时时间)
2.显示等待
显示等待是等待某个条件是否成立,如果不成立会在超时时间范围内容重复判断,知道条件成立或者超时为止,如果超时会报错!
使用方法:
1)创建等待对象: WebDriverWait(浏览器, 超时时间)
2)使用until或者until_not添加等待的条件
until - 等到指定条件成立为止
until_not - 等到指定条件不成立为止
等待对象.until(条件)
注意:条件必须是selenium中提供的条件
1)presence_of_element_located(标签) - 指定标签存在
2)element_to_be_clickable(标签) - 指定标签可点击
3)text_to_be_present_in_element(标签, 内容) - 指定标签的内容中包含指定内容
4)text_to_be_present_in_element_value(标签, 内容) - 指定标签的value属性值中包含指定内容
确定标签的方式: (By.Id, ‘q’) - 获取id值为q标签
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
b = Chrome()
# 1.设置隐式等待的超时时间
b.implicitly_wait(10)
b.get('https://www.51job.com')
# 2.设置显示等待
# 1)创建等待对象
wait = WebDriverWait(b, 30)
# 2)
# wait.until(EC.presence_of_element_located((By.ID, 'kwdselectid')))
# wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '.uname.e_icon.at'), '余婷'))
wait.until(EC.text_to_be_present_in_element_value((By.ID, 'kwdselectid'), '数据'))
print('==========数据分析搜索============')