day 007

day 007

1.异常处理

try:
  lt = [1, 2, 3]
  lt['a']
except Exception as e: # 万能异常
  print(e)
try:
  lt = [1, 2, 3]
  lt['a']
except Exception as e:
  print(e)
try:
  key = input('输入一个key获取字典中的值')
  dic = {'a': 1}
  dic[key] 
  except Exception as e:
  print('你输入有问题') 
n
i
c
k

h
a
n
d
s
o
m
e

try: ——implicitly_wait(5) # 防止网络延迟报错,让浏览器有5秒时间进行加载 ——get('要打开的网页') ——time.sleep(2) # 暂停两秒 ——driver.find_element_by_link_text('登录') # 通过文本查找登录按钮 ——click() # 点击 ——driver.find_element_by_id('id内容') # 通过id查找位置 ——send_keys('内容') # 将内容输入到定位的位置 —— time.sleep(10) finally: driver.close() # 无论是否出现错误到达时间后关闭该程序

driver = webdriver.Chrome(r'/Users/luocongyu/Downloads/百度网盘/上课数据/day 07/day 07 下午/chromedriver') # 启动selenium驱动打开Chrome浏览器

from selenium import webdriver # 用来驱动浏览器 from selenium.webdriver import ActionChains # 破解滑动验证码的时候用的 可以拖动图片 from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC # 和下面WebDriverWait一起用的 from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素T import time # 导入时间控制库

css_selector属性选择器查找

element:查找第一个 elements:查找所有

name: 值的input里面拥有的唯一属性

html标签查找 通过属性查找 优先使用: id: 值是唯一 class: 值可以有多个

安装selenium请求库 pip3 install selenium

http://npm.taobao.org/mirrors/chromedriver/2.38/

安装驱动

3.怎么使用selenium?

-缺点: 爬虫效率低

-优点: 通过它可以驱动浏览器,跳过登录滑动验证

2。为什么要使用selenium?

-一个自动化测试工具

1.什么是selenium

2.爬虫selenium请求库

s = 'nick nick nick' print(s.count('nick')) # 3

14.count() # 计数

s = 'a' print(s.isdigit()) # 判断是否为纯数字 # False print(s.isalpha()) # 判断是否为纯字母 # True

13.isdigit()/isalpha()

s='nick' print(s.center(50, '-')) # 居中 # -----------------------nick----------------------- print(s.ljust(50, '-')) # 居左 # nick---------------------------------------------- print(s.rjust(50, '-')) # 居又 # ----------------------------------------------nick

12.center/ljust/rjust : 打印更美观

s='a^b^c' print(s.split('^')) # ['a','b','c']

11.split() # 切割

print('^'.join(['a','b','c'])) # a^b^c

10.join() # 把列表内的元素拼接出来

s='nick' print(s.find('a')) # 找不到返回-1 # print(s.find('i')) # 1 print(s.index('a')) # 找不到报错 # print(s.index('c')) # 2

9.find()/index() # 获取某一元素的位置

s='nick' print(s.startswith('n')) # 以。。开头 # True print(s.endswith('k')) # 以。。结尾 # True

8.startswith()/endswith()

s='&&&nick^^' print(s.lstrip('&')) # 去掉左端的 # nick^^ print(s.lstrip('^')) # 去掉右端的 # &&&nick

7.lstrip()/rstrip()

s='*** nick handsome ' print(s.strip('n *')) # ick handsome # ('内的内容无顺序要求')

6.strip() # 默认去掉;两端空格

s='nick handsome' print(len(s)) # 13

5.len() # 长度

s='nick handsome' for i in s: print(i) #

4.for循环

s='nick handsome' print('nick h' in s) # True print('nick1' in s) # False

3.成员运算 # 判断('')是否在字符串内

s='nick handsome' print(s[0:4]) # nick

2.切片

s='nick handsome' print(s[0]) # n

1.索引取值

2.字符串的内置方法

try: 错误 错误 错误 except Exception as e:#万能异常 print(e)

输出结果自动;list indices must be integers or slices, not str 列表索引必须是整数或片,而不是字符串

原文地址:https://www.cnblogs.com/luocongyu/p/11437276.html