day - 6 爬虫流程整理
1. excel文件写操作
-
获取工作簿对象(工作簿对应的就是一个excel文件)
import openpyxl # 导入 # 新建(默认有一个工作表) work_book = openpyxl.Workbook() # 打开 work_book = openpyxl.load_workbook(文件路径) # work_book = openpyxl.Workbook() work_book = openpyxl.load_workbook('files/test2.xlsx')
-
获取所有的表的表名
work_book = openpyxl.load_workbook('files/test2.xlsx') print(all_names)
-
获取表
sheet = work_book['Sheet']
-
新建表
# 工作簿对象.create_sheet(表名, 表的下标) work_book.create_sheet('学生表') work_book.create_sheet('学生表2', 0)
-
删除表
# 工作簿对象.remove(表对象) work_book.remove(work_book[表名])
-
表重命名
# sheet = work_book['学生表2'] sheet = work_book.active sheet.title = 'Student' 重命名成 Student
-
写入数据到单元格
# 单元格对象.value - 获取单元格中的内容 # 单元格对象.value = 新值 - 修改单元格中的内容 sheet = work_book['Student'] # 1)获取单元格方法一 # 工作表对象.cell(行号: int, 列号: int) cell1 = sheet.cell(1, 1) # 修改单元格中的内容 cell1.value = '姓名' cell2 = sheet['B1'] cell2.value = '年龄' # 清空单元格 cell2.value = ''
最后保存文件
work_book.save('./files/test2.xlsx')
2.excel读文件操作
-
打开文件
import openpyxl wb = openpyxl.load_workbook('files/test1.xlsx')
-
获取工作表
# sheet = wb['学生表'] sheet = wb.active
-
获取单元格
# 1)获取单个单元格 sheet.cell(行号,列号) - 行号和列号都是从1开始的数字 sheet[位置信息] - 位置信息是类型:'A1' 、 'B2'的行列信息,其中字母是列信息,数字是行号 # 2)以行为单位获取单元格 iter_rows # 工作表对象.iter_rows(最小行号,最大行号,最小列号,最大列号) cells = sheet.iter_rows(1, 4, 1, 4) print(list(cells)) row_4 = sheet.iter_rows(4, 4) print(list(row_4)) cells = sheet.iter_rows(2, 4, 1, 2) print(list(cells)) # 3)以列为单位获取单元格 iter_cols cells = sheet.iter_cols(1, 4, 1, 4) print(list(cells)) all_scores = sheet.iter_cols(4, 4, 2, 4) print(list(all_scores)) for scre_cell in next(all_scores): # 取到的all_scores是迭代器, next是取迭代器中的元素 print(scre_cell.value) all_name = sheet.iter_cols(1, 1, 2, 2) print(list(all_name)) for name in next(all_name): print(name.value) all_age = sheet.iter_rows(2, 4) # 最小行号1 最大行号2 最小列号1 最大列号4 print(list(all_age)) for age in next(all_age): print(age.value)
3. 网易云邮件(html中的嵌套html)
from selenium import webdriver
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
b = webdriver.Chrome(options=options)
b.get('https://mail.163.com/')
# 需要爬取的内容在嵌套页面中(嵌套页面在iframe标签中),获取标签前需要切换页面
# 1.拿到嵌套页面对应的iframe标签
frame = b.find_element_by_css_selector('#loginDiv>iframe')
# 2.切换页面
b.switch_to.frame(frame)
# 3.获取嵌套页面中标签
user_name = b.find_element_by_name('email')
password = b.find_element_by_name('password')
login_btn = b.find_element_by_id('dologin')
user_name.send_keys('y_t209')
password.send_keys('123456')
login_btn.click()
4.selenium设置选项
from selenium import webdriver
# from selenium.webdriver import ChromeOptions
url = 'https://www.jd.com'
# 1.创建设置选项
options = webdriver.ChromeOptions()
# 2.添加选项参数
# 1) 取消测试环境
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 2) 取消图片加载(提高爬虫效率)
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
b = webdriver.Chrome(options=options)
b.get(url)
print(b.page_source)
5.frame切换
from selenium import webdriver
url = 'https://mail.163.com/'
b = webdriver.Chrome()
b.get(url)
"""
有的时候会遇到这样的网页:一个网页对应的html标签嵌套了其他的html标签
(前端如果要实现嵌套的功能必须要将被嵌套的html放在iframe标签中),
如果需要爬取网页内容在嵌套的html里面,需要先让浏览器选中内容嵌套的html。
(浏览器对象默认选中的是最外面的html标签)
"""
# 1. 获取提供html标签的iframe标签
box = b.find_element_by_css_selector('#loginDiv>iframe')
# 2.切换frame
b.switch_to.frame(box)
print(b.page_source)
6. 51job获取城市信息(点击添加城市信息)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# 打开网页
url = 'https://www.51job.com/'
b = webdriver.Chrome()
b.get(url)
# 自动搜索
input = b.find_element_by_css_selector('#kwdselectid')
input.send_keys('数据分析') # 在输入框中输入数据分析
input.send_keys(Keys.ENTER) # 点击搜索
allcity = b.find_element_by_css_selector('.allcity')
allcity.click()
print(b.page_source)
beijing = b.find_element_by_css_selector('.panel_lnp.panel_py.panel_ct2 .de.d3 tbody tr td em')
print(beijing.text)
beijing.click()
sure = b.find_element_by_css_selector('#popop > div > div.but_box > span')
sure.click()
7. 多线程技术
# 一个进程默认有一个线程,这个线程叫主线程。其他的线程(需要手动创建)都叫子线程。
# 如果一个Python程序需要子线程需要手动创建线程类Thread的对象
import time
from datetime import datetime
from threading import Thread
# Thread类 - 线程类 Thread类的对象 - 子线程
def download(name):
print(f'{name}开始下载:{datetime.now()}')
time.sleep(2)
print(f'{name}下载结束:{datetime.now()}')
# 1.在一个线程(主线程)中下载三个电影:消耗时间是6秒
# download('肖申克的救赎')
# download('霸王别姬')
# download('阿甘正传')
# 2.在三个子线程中分别下载三个电影
# 1)创建线程对象
t1 = Thread(target=download, args=('肖申克的救赎',))
t2 = Thread(target=download, args=('霸王别姬',))
t3 = Thread(target=download, args=('阿甘正传',))
# 2)启动线程
t1.start()
t2.start()
t3.start()