selenium总结
selenium的基本操作
from selenium.webdriver import Chrome
# 1. 创建浏览器对象
b = Chrome()
# 2.打开网页(需要爬哪个页面的数据,就打开哪个页面对应的网址)
b.get('https://movie.douban.com/top250?start=0&filter=')
# 3.获取网页源代码(注意:不管以什么方式更新了界面内容,page_sourse的页面也会更新)
print(b.page_source)
input('结束:')
selenium翻页
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
-
1.selenium获取多页数据翻页的方法:
1.找到不同页的地址变化规律,利用循环实现多页数据的请求 b = Chrome() print('----------------------方法1----------------') for x in range(0, 76, 25): b.get(f'https://movie.douban.com/top250?start=0&filter=') print(b.page_source) print('----------------------分割线----------------') print('----------------------分割线----------------') input()
2.点击翻页按钮,刷新页面内容,在刷新后获取网页源代码
b = Chrome()
b.get('https://movie.douban.com/top250?start=0&filter=')
for _ in range(5):
print(b.page_source)
# 点击下一页按钮
# b.find_element(By.CLASS_NAME, 'text')
# b.find_element(By.CSS_SELECTOR, '.text')
# b.find_element(By.PARTIAL_LINK_TEXT, '.text')
next = b.find_element(By.CLASS_NAME, 'next')
# 点击按钮
next.click()
-
方法2涉及的知识点
- selenium获取标签
浏览器对象.b.find_element(获取方式,数据) - 返回符合条件的第一个标签,结果是对象标签
浏览器对象.b.find_element(获取方式) - 返回符合条件的所有标签,结果是列表,列表中的元素是标签对象
- 获取方式:
By.ID - 通过ID属性值获取标签
By.CLASS_NAME - 通过class属性值获取标签
By.CSS_SELECTOR - 通过css选择器获取标签
By.LINK_TEXT - 通过a标签的标签内容获取标签(需要写全:获取a标签为xxx的标签)
By.PARTIAL_LINK_TEXT - 通过a标签的标签内容获取标签(不需要写全:获取a标签包含xxx的标签)
(1) 输入框输入内容:输入框对应的标签.send_keys(内容)
(2) 点击标签:标签对象.click()
from selenium.webdriver.common.by import By
b = Chrome()
b.get('https://www.jd.com/')
# 获取id属性值为key的标签
search = b.find_element(By.ID, 'key')
search.send_keys('电脑\n')
# 获取标签内容为'便宜包邮'的a标签
a1 = b.find_element(By.LINK_TEXT, '便宜包邮')
# a1.click()
# 获取标签内容包含'口好'的a标签
a2 = b.find_element(By.PARTIAL_LINK_TEXT, '口好')
a2.click()
input(':')
知网例题
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
from bs4 import BeautifulSoup
def analysis_data(html):
soup = BeautifulSoup(html, 'lxml')
digest = soup.select_one('#ChDivSummary').text
print(digest)
def get_net_data():
# 1.创建浏览器
b = Chrome()
# 2.打开中国知网
b.get('https://www.cnki.net/')
# 3.获取输入框,输入"数据分析"
search = b.find_element(By.ID, 'txt_SearchText')
search.send_keys('数据分析\n')
time.sleep(1)
# # 4.获取搜索结果所有论文的标题标签
titles = b.find_elements(By.CLASS_NAME, 'fz14')
# 点击第一个搜索结果
titles[0].click()
time.sleep(1)
# 切换选项卡,让浏览器对象指向详情页
b.switch_to.window(b.window_handles[-1])
# 获取详情页数据
print(b.page_source)
# 关闭当前窗口
b.close()
# 将选项卡切换回第一个页面
b.switch_to.window(b.window_handles[0])
titles[1].click()
input()
知网练习
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time
from bs4 import BeautifulSoup
def analysis_data(html):
soup = BeautifulSoup(html, 'lxml')
digest = soup.select_one('#ChDivSummary').text
print(digest)
def get_net_data():
# 1.创建浏览器
b = Chrome()
# 2.打开中国知网
b.get('https://www.cnki.net/')
# 3.获取输入框,输入"数据分析"
search = b.find_element(By.ID, 'txt_SearchText')
search.send_keys('数据分析\n')
time.sleep(1)
for _ in range(3):
# 4.获取搜索结果所有论文的标题标签
titles = b.find_elements(By.CLASS_NAME, 'fz14')
for x in titles:
# 点击一个搜索结果
x.click()
time.sleep(1)
# 切换选项卡,让浏览器对象指向详情页
b.switch_to.window(b.window_handles[-1])
# 获取详情页数据, 解析数据
# print(b.page_source)
analysis_data(b.page_source)
# 关闭当前窗口
b.close()
# 将选项卡切换回第一个页面
b.switch_to.window(b.window_handles[0])
print('--------------------一页数据获取完成--------------------------')
b.find_element(By.ID, 'PageNext').click()
time.sleep(4)
input()
if __name__ == '__main__':
get_net_data()
页面滚动
from selenium.webdriver import Chrome
import time
b = Chrome()
b.get('h')
time.sleep(2)
# input('人工滚动完成:')
print('----------------------用代码控制浏览器滚动----------------')
# js中页面滚动的代码:windows.scrollBy(x方向的偏移量,y方向的偏移量)
b.execute_script('scroll')
result = b.find_elements(By.CSS_SELECTOR,)
print(len(result))
input('结束:')