Python-异常处理 使用selenium库自动爬取数据
异常处理
处理程序的报错
语法
捕捉万能异常:
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库自动爬取数据的更多相关文章
- Python爬虫初探 - selenium+beautifulsoup4+chromedriver爬取需要登录的网页信息
目标 之前的自动答复机器人需要从一个内部网页上获取的消息用于回复一些问题,但是没有对应的查询api,于是想到了用脚本模拟浏览器访问网站爬取内容返回给用户.详细介绍了第一次探索python爬虫的坑. 准 ...
- 爬虫-----selenium模块自动爬取网页资源
selenium介绍与使用 1 selenium介绍 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. sel ...
- 通过python的urllib.request库来爬取一只猫
我们实验的网站很简单,就是一个关于猫的图片的网站:http://placekitten.com 代码如下: import urllib.request respond = urllib.request ...
- python模拟登陆知乎并爬取数据
一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...
- python网络爬虫(6)爬取数据静态
爬取静态数据并存储json import requests import chardet from bs4 import BeautifulSoup import json user_agent='M ...
- Python爬虫系列-Selenium+Chrome/PhantomJS爬取淘宝美食
1.搜索关键字 利用Selenium驱动浏览器搜索关键字,得到查询后的商品列表 2.分析页码并翻页 得到商品页码数,模拟翻页,得到后续页面的商品列表 3.分析提取商品内容 利用PyQuery分析源码, ...
- python通过token登录,并爬取数据实例
from bs4 import BeautifulSoup import requests class Zabbix(object): def __init__(self, headers): sel ...
- 如何手动写一个Python脚本自动爬取Bilibili小视频
如何手动写一个Python脚本自动爬取Bilibili小视频 国庆结束之余,某个不务正业的码农不好好干活,在B站瞎逛着,毕竟国庆嘛,还让不让人休息了诶-- 我身边的很多小伙伴们在朋友圈里面晒着出去游玩 ...
- Python使用urllib,urllib3,requests库+beautifulsoup爬取网页
Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...
随机推荐
- Eclipse MyBatis generator 1.3.7插件的核心包(中文注释)
一.最近刚搭建一个项目框架,使用springboot + mybatis,但是在使用Eclipse开发时发现开发mybatis的Dao.mapper.xml和entity时特别不方便,手工去写肯定是不 ...
- System.Web.HttpContext.cs
ylbtech-System.Web.HttpContext.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken ...
- error LNK1104: 无法打开文件“C:\Users\Administrator\Desktop\....\\xxxx.exe”
1.错误原因:项目在在打开前已经打开了win32里面的程序,并且运行. 解决方法:关闭直接在win32中运行的程序,再点击调试 2.暂时不知道原因,打开程序还会报无权限创建可执行文件.如下图所示 解 ...
- ims注册与注销
ims注册流程: 步骤1:当IMS和SIM访问已经就绪时,IMC将在满足条件后触发IMS注册到IMCB.步骤2.3:IMCB请求IMSA/IMSM获取IMS PDN和P-CSCF,然后触发VoLTE ...
- <每日一题>算法题:集合求并集并排序
题目描述 给你两个集合,要求{A} + {B}. 注:同一个集合中不会有两个相同的元素. 输入描述: 每组输入数据分为三行,第一行有两个数字n,m(0 ≤ n,m ≤ 10000),分别表示集合A和集 ...
- (转)行为树(Behavior Tree)实践(1)– 基本概念
通过一个例子来介绍一下行为树的基本概念,会比较容易理解,看下图: 这是我们为一个士兵定义的一颗行为树(可以先不管这些绿圈和红圈是干吗的),首先,可以看到这是一个树形结构的图,有根节点,有分支,而且子节 ...
- Educational Codeforces Round49
A Palindromic Twist(字符串) 问每个字母必须向左或向右变成另一个字母,问能不能构成回文 #include <iostream> #include <string. ...
- Android开发 MediaRecorder使用Camera2配合录制视频(暂时有异常抛出,无法使用)
前言 这个博客本来是用来详细介绍MediaRecorder与Camera2,但是出乎预料之外,在获取mMediaRecorder.getSurface();的时候无论如何都是报错的,报错为Surfac ...
- 记 openSUSE 42.3 升级到Leap 15.0
先将系统的软件更新到最新版本 sudo zypper update 删除42.3的软件源,若有其他数据源,如nginx的,也需要一并删除,可使用zypper lr -d 来查询 sudo zypper ...
- input判断输入值是否合法
1.判断input输入的值是否合法有很多办法,我这里使用的是在onchange时进行判断,代码如下:[所有主要浏览器都支持] <input type="text" name= ...