python3.8
一、任务要求
学习 Selenium自动化测试框架,在Anaconda的一个虚拟环境下安装selenium 和webdrive等必要库。熟练掌握在浏览器的开发者模式(Chrome 和Edge 浏览器按F12 )下对网页结构进行分析,找到对应网页元素的技能。然后完成下列任务:
1)对一个网页进行自动化测试。比如自动填充百度网页的查询关键字,完成自动搜索。
2)到http://quotes.toscrape.com/js/网站去爬取十句名言
3) 爬取京东网站上的感兴趣书籍信息(如关键字“python编程”的前200本图书),并保存。
二、任务一——自动打开百度并搜索
- 查看百度网页源码找到搜索框id以及搜索按钮id
-
导入相应包
要使用selenium去调用浏览器,还需要一个驱动,不同浏览器的webdriver需要独立安装,
如果是chrome在浏览器输入框输入chrome://version/ 查看相应版本,http://npm.taobao.org/mirrors/chromedriver/下载相应驱动即可 -
代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
s = Service("I:\python\myProjects\crawler\chromedriver.exe")# 驱动path
driver = webdriver.Chrome(service=s)
driver.get("https://www.baidu.com/")
# search=driver.find_element_by_id("kw")这种方式已经被弃用
search=driver.find_element(By.ID,"kw")
search.send_keys("伊木子曦")
# send_button=driver.find_element_by_id("su")
send_button=driver.find_element(By.ID,"su")
send_button.click()
注意: driver.find_element_by_id()有些不能用了,这个是版本更新,语法不适用新版python。
新版find_element需要引入by包,大家注意一下我的是python3.8。
find_element():只查找一个页面元素,方法返回值为WebElement对象;
find_elements():查找页面上所有满足定位条件的元素,方法返回值为WebElement对象的列表
- 效果展示
三、任务二——网站爬取名言
- 查看网页源码,分析源码信息,找到对应标签
- 代码
# 爬取 http://quotes.toscrape.com/js/ 名人名言
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
s = Service("I:\python\myProjects\crawler\chromedriver.exe")
# 名言所在网站
driver = webdriver.Chrome(service=s)
driver.get("http://quotes.toscrape.com/js/")
# 表头
csvHeaders = ['作者','名言']
# 所有数据
subjects = []
# 单个数据
subject=[]
# 获取所有含有quote的标签
# res_list=driver.find_elements_by_class_name("quote")# 有些不能用了,这个是版本更新,下面是我适应的
res_list=driver.find_elements(By.CLASS_NAME, "quote")
# 分离出需要的内容
for tmp in res_list:
subject.append(tmp.find_element(By.CLASS_NAME, "author").text)
subject.append(tmp.find_element(By.CLASS_NAME, "text").text)
print(subject)
subjects.append(subject)
subject=[]
- 效果展示
四、任务三——爬取京东图书
- 查看https://www.jd.com/网页源码,分析源码信息,找到对应标签
查看搜索框id以及搜索按钮class
- 代码
# 爬取 https://www.jd.com/ 京东图书
import csv
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
s = Service("I:\python\myProjects\crawler\chromedriver.exe")
# 京东所在网站
driver = webdriver.Chrome(service=s)
driver.set_window_size(1920,1080)
driver.get("https://www.jd.com/")
# 输入需要查找的关键字
p_input = driver.find_element(By.ID, 'key')
p_input.send_keys('python编程') # 找到输入框输入
time.sleep(1)
# 点击搜素按钮
button=driver.find_element(By.CLASS_NAME,"button").click()
time.sleep(1)
all_book_info = []
num=200
head=['书名', '价格']
#csv文件的路径和名字
path='./file/book.csv'
def write_csv(head,all_book_info,path):
with open(path, 'w', newline='',encoding='utf-8') as file:
fileWriter = csv.writer(file)
fileWriter.writerow(head)
fileWriter.writerows(all_book_info)
# 爬取一页
def get_onePage_info(num):
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
time.sleep(2)
# 书籍列表
J_goodsList = driver.find_element(By.ID, "J_goodsList")
listbook = J_goodsList.find_elements(By.TAG_NAME, "li")
for res in listbook:
num = num-1
book_info = []
name =res.find_element(By.CLASS_NAME, "p-name").find_element(By.TAG_NAME, "em").text
price = res.find_element(By.CLASS_NAME, "p-price").find_element(By.TAG_NAME, "i").text
book_info.append(name)
book_info.append(price)
# bookdetail = res.find_element(By.CLASS_NAME, "p-bookdetails")
# author = bookdetail.find_element(By.CLASS_NAME, "p-bi-name").find_element(By.TAG_NAME, "a").text
# store = bookdetail.find_element(By.CLASS_NAME, "p-bi-store").find_element(By.TAG_NAME, "a").text
# book_info.append(author)
# book_info.append(store)
all_book_info.append(book_info)
if num==0:
break
return num
while num!=0:
num = get_onePage_info(num)
driver.find_element(By.CLASS_NAME, 'pn-next').click() # 点击下一页
time.sleep(2)
write_csv(head, all_book_info, path)
driver.close()
- 效果展示
五、参考
https://zhuanlan.zhihu.com/p/331712873
https://blog.csdn.net/m0_62298204/article/details/120802053