异常处理

处理程序的报错

语法

捕捉万能异常:

try:
print(a)
except Exception as e:
print("你的代码有问题")
print("程序走下面的代码")

字符串内置方法

  • 索引取值
  • 切片
  • 长度(len)
  • 成员运算
  • 移除两边空白字符
  • str切分
  • 循环
  • startswith/endswith
  • join()
  • index()
  • count()

selenium

  • 是一个自动化测试工具,可以通过驱动浏览器,自动点击完成功能

  • 安装驱动

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

    安装请求库

    pip3 install selenium

首先体验一下selenium的效果,使用驱动,自动打开浏览器进入百度,代码:

# coding=utf-8
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 # 等待页面加载某些元素
import time drive = webdriver.Chrome(r"C:\Users\Administrator\Desktop\chromedriver.exe") try:
#等待浏览器加载10s
drive.implicitly_wait(10) #打开浏览器访问百度页面
drive.get("https://www.baidu.com/")
time.sleep(1) #找到搜索框
search_button = drive.find_element_by_id("kw")
search_button.send_keys("驱动") #找到 百度一下 按钮
baiduyixia_button = drive.find_element_by_id("su")
baiduyixia_button.click() time.sleep(10) finally:
drive.close()

爬取京东商品并把数据保存下来

# coding=utf-8
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 # 等待页面加载某些元素
import time drive = webdriver.Chrome(r"C:\Users\Administrator\Desktop\chromedriver.exe")
#
try:
#等待浏览器加载
drive.implicitly_wait(10) #搜索京东
drive.get("https://www.jd.com/")
search_button = drive.find_element_by_id("key")
search_button.send_keys("全新国行iPhone8") #找到 搜索 按钮 或者回车搜索,没有找到搜索按钮, 直接回车
search_button.send_keys(Keys.ENTER) #通过id查找商品的父标签
goods_div = drive.find_element_by_id("J_goodsList")
#通过属性名找每个商品的具体信息
goods_list = goods_div.find_elements_by_class_name("gl-item")
print(type(goods_list))
# # #通过循环出 每个商品的详情
for goods in goods_list:
# 通过css_selector获取商品价格
goods_price = goods.find_element_by_css_selector('.p-price i').text
# 通过css_selector获取商品名称
goods_name = goods.find_element_by_css_selector('.p-name em').text
# 通过css_selector获取商品评价人数
goods_commit = goods.find_element_by_css_selector('.p-commit a').text
# 通过css_selector获取商品详情链接
goods_url = goods.find_element_by_css_selector('.p-commit a').get_attribute('href') data = f'''
商品名称:{goods_name}
商品价格:{goods_price}
评价人数:{goods_commit}
详情链接:{goods_url}
'''
print(data) with open("京东手机信息.txt","a",encoding="utf8") as f:
f.write(data) time.sleep(10) finally:

自动登录百度账号

# coding=utf-8
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 # 等待页面加载某些元素
import time drive = webdriver.Chrome(r"C:\Users\Administrator\Desktop\chromedriver.exe")
# try:
drive.implicitly_wait(10) drive.get("https://www.baidu.com/")
login_button = drive.find_element_by_link_text("登录")
login_button.click() login_tag = drive.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn")
login_tag.click() login_tag_user = drive.find_element_by_id("TANGRAM__PSP_10__userName")
login_tag_user.send_keys("15221024542")
login_tag_pass = drive.find_element_by_id("TANGRAM__PSP_10__password")
login_tag_pass.send_keys("123456789qq") login_commit = drive.find_element_by_id("TANGRAM__PSP_10__submit")
login_commit.click() time.sleep(10)
finally:
drive.close()

Python-异常处理 使用selenium库自动爬取数据的更多相关文章

  1. Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息

    目标 之前的自动答复机器人需要从一个内部网页上获取的消息用于回复一些问题,但是没有对应的查询api,于是想到了用脚本模拟浏览器访问网站爬取内容返回给用户.详细介绍了第一次探索python爬虫的坑. 准 ...

  2. 爬虫-----selenium模块自动爬取网页资源

    selenium介绍与使用 1 selenium介绍 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作.     sel ...

  3. 通过python的urllib.request库来爬取一只猫

    我们实验的网站很简单,就是一个关于猫的图片的网站:http://placekitten.com 代码如下: import urllib.request respond = urllib.request ...

  4. python模拟登陆知乎并爬取数据

    一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...

  5. python网络爬虫(6)爬取数据静态

    爬取静态数据并存储json import requests import chardet from bs4 import BeautifulSoup import json user_agent='M ...

  6. Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食

    1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...

  7. python通过token登录,并爬取数据实例

    from bs4 import BeautifulSoup import requests class Zabbix(object): def __init__(self, headers): sel ...

  8. 如何手动写一个Python脚本自动爬取Bilibili小视频

    如何手动写一个Python脚本自动爬取Bilibili小视频 国庆结束之余,某个不务正业的码农不好好干活,在B站瞎逛着,毕竟国庆嘛,还让不让人休息了诶-- 我身边的很多小伙伴们在朋友圈里面晒着出去游玩 ...

  9. Python使用urllib,urllib3,requests库+beautifulsoup爬取网页

    Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...

随机推荐

  1. 剑指Offer-13:调整数组位置使奇数位于偶数前面

    题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变.例如给定一个数组 ...

  2. C++——虚析构

    目的: //只执行了 父类的析构函数//向通过父类指针 把 所有的子类对象的析构函数 都执行一遍//向通过父类指针 释放所有的子类资源 方法:在父类的析构函数前+virtual关键字 #define ...

  3. t检验中的t值和p值是什么关系_t检验和p值的关系

    t检验中的t值和p值是什么关系_t检验和p值的关系 t检验中通过样本均值 总体均值 样本标准差 样本量 可以计算出一个t值,这个t值和p值有什么关系? 根据界值表又会查出一个数,这个数和t值比较,得出 ...

  4. MDK 虚拟串口 *** error 30: undefined name of virtual register

    概念说明 查看已有的虚拟寄存器 输入指令: dir vtreg 可以看到没有要配置的虚拟寄存器SxIN和SxOUT,通过查询手册可以看到所有的虚拟寄存器类型: 说明不支持.

  5. POJ-1260-Pearls-dp+理解题意

    In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jew ...

  6. day 82 Vue学习三之vue组件

      Vue学习三之vue组件   本节目录 一 什么是组件 二 v-model双向数据绑定 三 组件基础 四 父子组件传值 五 平行组件传值 六 xxx 七 xxx 八 xxx 一 什么是组件 首先给 ...

  7. SQL 分组后获取每组中最大值

    场景:sql server 2008 drop table ID CREATE TABLE ID ( id ,) not null, code int , D date, PRIMARY KEY (i ...

  8. 第十五篇:java操作oracle踩坑之旅

    最近刚做完mysql的各种需求,项目要满足oracle数据库,于是走上了漫漫的踩坑之路,同行可以看看以免踩坑……第一条:oracle建表的时候不需要在建表sql语句后指定默认字符集 DEFAULT C ...

  9. CentOS7-Minimal安装MySQL服务

    CentOS7默认安装的是Mariadb而不是mysql,而Mariadb是mysql的一个分支, 安装mysql会覆盖Mariadb 一.下载MySQL官方的 Yum Repository [roo ...

  10. WPF基础之Grid面板

    一.显示 Grid的线条,设置ShowGridLiens="True".