异常处理,常用字符串方法,selenium模块
异常处理
什么是异常处理 (处理异常,报错error)
print(1 / 0) # 报了0除错误
# 打印结果:
Traceback (most recent call last):
File "D:/pycharm_project/day07/01异常处理.py", line 18, in <module>
print(1 / 0)
ZeroDivisionError: division by zero
捕捉异常
try:
print(1 / 0) # 有错误就跳过,没错误就执行
except ZeroDivisionError: # 错误被捕捉了
pass # 啥也不做
print(1)
这种方法只能捕捉这个指定错误, 当然也有其他错误
try:
print(x)
print(1 / 0) # 有错误就跳过,没错误就执行
except ZeroDivisionError: # 错误被捕捉了
pass # 啥也不做
except NameError:
pass
print(1)
这样很麻烦,异常有很多种
try:
print('--------')
print(a)
except Exception as e: # 把错误信息输入出来,同时一般把错误信息记录到日志
print(e)
print('--------')
# 打印结果:
--------
name 'a' is not defined
--------
这样就用Exception 就能自动识别错误,很方便~
字符串常用方法
索引取值
s = 'hello'
print(s[0]) # 取到h
切片
s = 'hello world'
print(s[0:5]) # 取到hello
成员运算
s = 'hello'
print('h' in s) # 打印True
for循环
s = 'hello'
for i in s:
print(i)
# 打印:
h
e
l
l
o
字符串长度
s = 'hello'
print(len(s)) # 打印字符串长度 5
strip()
s = '** hello '
print(s.strip()) # 默认去掉两端空格
print(s.strip('*')) # 去掉*
print(s.strip('* h')) # 去掉* h
l.strip() 和 r.strip()
s = '**cwz**'
print(s.lstrip('*')) # 去掉左边*
print(s.rstrip('*')) # 去掉右边*
startswith()和endswith()
s = 'hello'
print(s.startswith('h')) # True
print(s.endswith('o')) # True
find() 寻找索引位置
s = 'hello'
print(s.find('h')) # 打印0
print(s.find('x')) # 字符串中没有的返回-1
print(s.find('l')) # 字符串中有相同的,找第一个 返回2
index() 索引位置
s = 'hello'
print(s.index('o')) # 打印4
print(s.index('a')) # 字符串中没有的直接报错
join() 把列表中元素拼接起来
lt = ['a','b','c']
print('*'.join(lt))
# 打印结果:
a*b*c
split() 切割
s = 'a*b*c'
print(s.split('*')) # 以*切割
# 打印结果:
['a', 'b', 'c']
replace 替换
s = 'reese neo'
print(s.replace('reese','cwz'))
# 打印结果:
cwz neo
center/ljust/rjust 居中/居左/居右
s = 'hello'
print(s.center(20,'-'))
print(s.ljust(20,'-'))
print(s.rjust(20,'-'))
# 打印结果:
-------hello--------
hello---------------
---------------hello
isdigit() 和 isalpha()
s = '123'
print(s.isdigit()) # 判断是否全为数字 True
print(s.isalpha()) # 判断是否全为字母 Flase
count 计数
s = 'aabcda'
print(s.count('a')) # 打印3
selenium模块
什么是selenium
selenium是一个自动化测试工具
为什么要用selenium
- 通过selenium可以驱动浏览器,跳过登录滑动验证
- 缺点是爬虫效率低下
怎么使用selenium
- 安装驱动chromedriver http://npm.taobao.org/mirrors/chromedriver/
- 安装selenium pip3 install selenium
selenium基本使用
from selenium import webdriver # 用来驱动浏览器
import time
# webdriver.Chrome('驱动绝对路径')
driver = webdriver.Chrome(r'E:\chromedriver_win32\chromedriver.exe')
driver.get('https://www.baidu.com')
time.sleep(10) # 等待10s
driver.close() # 关闭驱动
selenium驱动浏览器输入
from selenium import webdriver
import time
try:
driver = webdriver.Chrome(r'E:\chromedriver_win32\chromedriver.exe')
driver.get('https://www.baidu.com') # 打开百度
# 通过id查找输入框
input_tag = driver.find_element_by_id('kw')
input_tag.send_keys('四驱车') # 输入查找内容
# 通过id查找百度一下按钮
submit_button = driver.find_element_by_id('kw')
submit_button.click() # 点击百度一下按钮
time.sleep(10)
finally: # 无论有没有异常,都会执行下面的代码,关闭驱动
driver.close() # 关闭驱动
selenium模拟登录
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys # 键盘按键操作
driver = webdriver.Chrome(r'E:\chromedriver_win32\chromedriver.exe')
try:
driver.implicitly_wait(10) # 等待浏览器加载数据10s
driver.get('https://leetcode-cn.com/')
# 通过文本查找登录按钮
login_button = driver.find_element_by_link_text('登录')
login_button.click()
# 通过class查找用户名输入框
username = driver.find_element_by_class_name('css-paawy7-BasicInput')
username.send_keys('123456789@qq.com')
# 通过name查找密码输入框
password = driver.find_element_by_name('password')
password.send_keys('123456')
password.send_keys(Keys.ENTER) # 直接按回车
time.sleep(10)
finally:
driver.close()
selenium爬取京东商品信息
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys # 键盘按键操作
driver = webdriver.Chrome(r'E:\chromedriver_win32\chromedriver.exe')
try:
driver.implicitly_wait(10) # 等待浏览器加载数据10s
driver.get('https://www.jd.com/')
# 通过id号查找输入框
goods_input = driver.find_element_by_id('key')
goods_input.send_keys('笔记本电脑')
# 操纵键盘 按回车键
goods_input.send_keys(Keys.ENTER) # 直接按回车搜索
time.sleep(3) # 等待数据加载完成
# 通过id查找所有商品的父标签
goods_div = driver.find_element_by_id('J_goodsList')
# 通过class 标签查找goods_div下的所有li标签
goods_list = goods_div.find_elements_by_class_name('gl-item')
# print(goods_list)
# 循环 获取li标签
for goods in goods_list:
# 获取商品价格文本
# goods_price = goods.find_element_by_link_text('p-price').text
# css属性选择器
goods_price = goods.find_element_by_css_selector('.p-price i').text
goods_name = goods.find_element_by_css_selector('.p-name em').text
goods_commit = goods.find_element_by_css_selector('.p-commit a').text
goods_url = goods.find_element_by_css_selector('.p-name a').get_attribute('href')
goods_data = f'''
商品名称:{goods_name}
商品价格:{goods_price}
评价人数:{goods_commit}
详情链接:{goods_url}
'''
with open('笔记本电脑.txt', 'a', encoding='utf8') as f:
f.write(goods_data)
time.sleep(10)
finally:
driver.close()
异常处理,常用字符串方法,selenium模块的更多相关文章
- Python学习之==>常用字符串方法
1.常用字符串方法 a = '\n 字 符 串 \n\n' b = a.strip() # 默认去掉字符串两边的空格和换行符 c = a.lstrip() # 默认去掉字符串左边的空格和换行符 d = ...
- 48-python基础-python3-字符串-常用字符串方法(六)-strip()-rstrip()-lstrip()
7-用 strip().rstrip()和 lstrip()删除空白字符 strip()字符串方法将返回一个新的字符串,它的开头或末尾都没有空白字符. lstrip()和 rstrip()方法将相应删 ...
- 47-python基础-python3-字符串-常用字符串方法(五)-rjust()-ljust()-center()
6-rjust().ljust()和 center()方法对齐文本 rjust()和 ljust()字符串方法返回调用它们的字符串的填充版本,默认通过插入空格来对齐文本. rjust()和 ljust ...
- 44-python基础-python3-字符串-常用字符串方法(二)-isalpha()-isalnum()-isdigit()-isspace()-istitle()
3-isX 字符串方法 序号 方法 条件 返回结果1 返回结果2 1 isalpha() 如果字符串只包含字母,并且非空; True False 2 isalnum() 如果字符串只包含字母和数字 ...
- 43-python基础-python3-字符串-常用字符串方法(一)-upper()-lower()-isupper()-islower()
请注意, 这些方法没有改变字符串本身,而是返回一个新字符串. 如果你希望改变原来的字符串,就必须在该字符串上调用 upper()或 lower(),然后将这个新字符串赋给保存原来字符串的变量. 1 ...
- 【java】开发中常用字符串方法
java字符串的功能可以说非常强大, 它的每一种方法也都很有用. java字符串中常用的有两种字符串类, 分别是String类和StringBuffer类. Sting类 String类的对象是不可变 ...
- 第1章 Java中常用字符串方法总结
1.1 charAt方法——提取指定字符 1.2 codePointAt方法——提取索引字符代码点 1.3 codePointBefore方法——获取索引前一个字符的代码点 1.4 codePoint ...
- JavaScript基础进阶之常用字符串方法总结
前面三篇文章简单的把JavaScript基础内容过了一遍,我们已经可以用JavaScript写一些简单的代码了. 今天主要总结一下JavaScript中String对象中自带的一些方法,来帮助我们处理 ...
- JavaSctipt 常用字符串 方法及使用方式
1. charAt(x) charAt(x)返回字符串中x位置的字符,下标从 0 开始. //charAt(x) var myString = 'jQuery FTW!!!'; console.log ...
随机推荐
- 安装fiddler后,willow安装
willow 安装需要与fiddler安装在同一个磁盘,如果出现报错找不到路径,请按下面地址下载willow后重新安装 willow下载地址: https://github.com/QzoneTouc ...
- firefox56 版本中的 Selenium IDE 无法导出脚本问题
firefox:56 Slenium IDE :3系列 问题:Selenium IDE 没有工具栏,无法导出录制的脚本,这给自动化测试工作带来了极大的不便. 解决办法:将firefox 降级 (只有5 ...
- 【bzoj4559】[JLoi2016]成绩比较(dp+拉格朗日插值)
bzoj 题意: 有\(n\)位同学,\(m\)门课. 一位同学在第\(i\)门课上面获得的分数上限为\(u_i\). 定义同学\(A\)碾压同学\(B\)为每一课\(A\)同学的成绩都不低于\(B\ ...
- luoguP3531 [POI2012]LIT-Letters
(https://www.luogu.org/problem/P3531) 注意编号 #include<cstdio> #include<algorithm> #include ...
- keeplived+lvs(主从热备+负载均衡)
本次实验基于DR负载均衡模式(直接路由),设置一个VIP(Virtual IP)为192.168.1.225,用户只需要访问这个IP地址即可获得网页服务.其中,负载均衡主机为192.168.1.221 ...
- 巡风扫描器web界面工作流程
这两周学习了巡风扫描器的搭建,也在学长的带领下看了各部分的下源代码,为了加深记忆,梳理一下巡风大体的工作流程,主要通过web端的页面分析,错误的地方还请大佬们多多指正. 整体看一下巡风的扫描流程:登陆 ...
- 【转】HTTPS建立连接的过程
原文链接:https://www.cnblogs.com/shiqi17/p/9756880.html https://www.jianshu.com/p/bd75ab32ae57 HTTP建立连接的 ...
- LG3237 「HNOI2014」米特运输 树形DP
问题描述 LG3237 题解 问题转化为: 要求将这棵树,满足 结点 \(x\) 所有孩子权值相等 结点 \(x\) 权值等于所有孩子权值和 将乘法转化为 \(\log\) 加法 \(\mathrm{ ...
- UVA11464 Even Parity 搜索+递推
问题描述 UVA11464 题解 第一直觉爆搜. 发现 \(N \le 15\) ,然后后面每行都可以通过第一行递推出来. 爆搜第一行,递推后面+check \(\mathrm{Code}\) #in ...
- NOIP模拟赛 迷路
题目描述 Description \(FYH\) 在 \(ns\) 星系迷路了,情急之下,他找到了你.现在,解救 \(FYH\) 的重任就落在了你的肩上了. \(ns\) 星系有 \(n\) 颗星球, ...