异常处理

处理程序的报错

语法

捕捉万能异常:

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. 【luoguP3868】猜数字

    description 现有两组数字,每组k个,第一组中的数字分别为:a1,a2,...,ak表示,第二组中的数字分别用b1,b2,...,bk表示.其中第二组中的数字是两两互素的.求最小的非负整数n ...

  2. LUOGU P4394 [BOI2008]Elect 选举 (背包)

    传送门 解题思路 一眼看上去就像个背包,然后就是\(0/1\)背包改一改,结果发现过不了样例.后来想了一下发现要按\(a\)从大到小排序,因为如果对于一个>=总和的一半但不满足的情况来说,把最小 ...

  3. mysql 新特性之geometry

    1.获取矩形两个点的数据(左上角和右下角) SELECT  *    FROM    t_location    WHERE   MBRContains                    (    ...

  4. 如何有效管理Windows系统帐户权限

    权限是Windows管理的基础,当然与Windows用户关系最密切,平时接触最多的是与帐户相关的权限.对于Windows帐户权限的管理,你是否完全了解呢?下面,笔者以Winsows XP为例进行相关测 ...

  5. clientHeight / scrollHeight / offsetHeight 等属性的区别图

    网页(内容)可见区域宽:document.body.clientWidth 网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一 ...

  6. 在Laravel5.4中自动加载自定义文件

    目标:想要在TestController.php中使用自定义的/app/Common/test.php中的test()函数. 1.在app文件夹下创建文件app/Common/test.php,文件内 ...

  7. ps快速将白底图片变为透明图片

    方法一: 如果图层有锁图标,则要点击它,然它消失.然后选中魔棒工具,然后点击图片上要透明的区域,按下backspace键即可. 方法二: 转载自:https://blog.csdn.net/sunyi ...

  8. multiprocessing多进程(31-04)创建进程的两种方式

    一个进程可以寄生多个线程. CPU核数与进程个数是统一的, 若进程多于核数,那么只有等待上一进程执行完才能被执行. ------------------第一种进程创建方式--------------- ...

  9. Spring Boot 实现定时任务的 4 种方式

    作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...

  10. Xcode9.4.1官方下载链接地址

    More Downloads for Apple Developershttps://developer.apple.com/download/more/ Xcode 9.4.1https://dow ...