selenium+headless chrome爬虫的实现示例
作者:guduyishuai 发布时间:2022-01-31 19:29:50
标签:selenium,headless,chrome,爬虫
python爬虫写起来非常快,虽然也可以用java,但是没有python来的简洁迅速
selenium在前面总结过,是一个自动化测试库。headless chrome是 * 面的浏览器模式,和PHANTOMJS类似。但是PHANTOMJS往往会出现莫名的错误,而且速度没有headless chrome快
from selenium.webdriver.chrome.options import Options
global DRIVER
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
DRIVER = webdriver.Chrome(chrome_options=chrome_options)
爬虫的代码有一点需要注意,需要操作事件的时候最好不要直接用相应的方法,比如click。最好嵌入js脚本的方式进行调用。因为爬虫的代码执行速度很快,前端元素结构往往反应不过来,从而找出元素不可见或者不存在的错误。
province_items = DRIVER.find_element_by_class_name("city-province").find_elements_by_tag_name("a")
#province_item.click()
DRIVER.execute_script('arguments[0].click();',province_item)
下面来个例子,由于做电商平台,省、市、区的数据很好找,但是没有镇、街道的信息。这里通过爬虫从淘宝网将镇,街道的信息抓取下来
#! /usr/local/bin/python
# encoding: utf-8
'''
Created on 2018年1月5日
@author: wulinfeng
@date: 2018-1-5
'''
import time
#import request
from selenium import webdriver
#from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
import pymysql
def init_db():
global CONNECTION
CONNECTION = pymysql.connect("地址","用户名","密码","数据库" ,use_unicode=True, charset="utf8")
def init_web_driver():
global DRIVER
#DRIVER = webdriver.PhantomJS(executable_path='C:\phantomjs-1.9.2-windows\phantomjs.exe')
#DRIVER.set_window_size(1920, 1080)
'''
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
)
dcap["phantomjs.page.settings.viewportSize"] = (
"width: 1920, "
"height: 1080"
)
DRIVER = webdriver.PhantomJS(executable_path='C:\phantomjs-1.9.2-windows\phantomjs.exe',desired_capabilities=dcap)
DRIVER.set_window_size(1920, 1080)
'''
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
DRIVER = webdriver.Chrome(chrome_options=chrome_options)
#DRIVER=webdriver.Ie()
#DRIVER=webdriver.Chrome()
def close_db():
CONNECTION.close()
def close_web_driver():
DRIVER.quit()
def login_taobao(username, password):
DRIVER.get("https://member1.taobao.com/member/fresh/deliver_address.htm?spm=a1z08.2.0.0.7dad47611Wnj46")
#DRIVER.get("https://login.taobao.com/member/login.jhtml?spm=a21bo.2017.201864-2.d1.7d2082a4FxukGr&f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F")
#选择登陆方式
DRIVER.find_element_by_xpath("//*[@id=\"J_Quick2Static\"]").click()
#登陆
input_user = DRIVER.find_element_by_xpath("//*[@id=\"TPL_username_1\"]")
input_user.clear()
input_user.send_keys(username)
DRIVER.find_element_by_xpath("//*[@id=\"TPL_password_1\"]").send_keys(password)
DRIVER.find_element_by_xpath("//*[@id=\"J_SubmitStatic\"]").click();
time.sleep(0.5)
def get_data():
#点击地址选择
#DRIVER.find_element_by_xpath("//*[@id=\"city-title\"]").click()
city_title = DRIVER.find_element_by_id("city-title")
DRIVER.execute_script('arguments[0].click();',city_title)
get_province_and_sub()
def get_province_and_sub():
#获得省列表
province_items = DRIVER.find_element_by_class_name("city-province").find_elements_by_tag_name("a")
for province_item in province_items:
pid = province_item.get_attribute("attr-id");
pname = province_item.get_attribute("title");
if pid == "-1":
print("continue province")
continue
sql = "insert into region_province_t (province_id,province) values('"+pid+"','"+pname+"')"
print(sql)
cursor = CONNECTION.cursor()
cursor.execute(sql)
CONNECTION.commit()
#province_item.click()
DRIVER.execute_script('arguments[0].click();',province_item)
time.sleep(0.5)
get_city_and_sub(pid)
back_tab(0)
def get_city_and_sub(pid):
#获得市列表
city_items = DRIVER.find_element_by_class_name("city-city").find_elements_by_tag_name("a")
for city_item in city_items:
cid = city_item.get_attribute("attr-id");
cname = city_item.get_attribute("title");
if cid == "-1":
print("continue city")
continue
sql = "insert into region_city_t (city_id,city,province_id) values('"+cid+"','"+cname+"','"+pid+"')"
print(sql)
cursor = CONNECTION.cursor()
cursor.execute(sql)
CONNECTION.commit()
#city_item.click()
DRIVER.execute_script('arguments[0].click();',city_item)
time.sleep(1)
get_area_and_sub(cid)
back_tab(1)
def get_area_and_sub(cid):
#获得县区列表
area_items = DRIVER.find_element_by_class_name("city-district").find_elements_by_tag_name("a")
for area_item in area_items:
aid = area_item.get_attribute("attr-id");
aname = area_item.get_attribute("title");
if aid == "-1":
print("continue area")
continue
sql = "insert into region_area_t (area_id,area,city_id) values('"+aid+"','"+aname+"','"+cid+"')"
print(sql)
cursor = CONNECTION.cursor()
cursor.execute(sql)
CONNECTION.commit()
#area_item.click()
DRIVER.execute_script('arguments[0].click();',area_item)
time.sleep(0.5)
get_town_and_sub(aid)
back_tab(2)
def get_town_and_sub(aid):
#获得镇列表
town_items = DRIVER.find_element_by_class_name("city-street").find_elements_by_tag_name("a")
for town_item in town_items:
tid = town_item.get_attribute("attr-id");
tname = town_item.get_attribute("title");
if tid == "-1":
print("continue town")
continue
sql = "insert into region_town_t (town_id,town,area_id) values('"+tid+"','"+tname+"','"+aid+"')"
print(sql)
cursor = CONNECTION.cursor()
cursor.execute(sql)
CONNECTION.commit()
def back_tab(index):
districtEle = DRIVER.find_element_by_class_name("city-select-tab").find_elements_by_tag_name("a")[index]
DRIVER.execute_script('arguments[0].click();',districtEle)
time.sleep(0.5)
init_db()
init_web_driver()
login_taobao("用户名", "密码")
get_data()
close_db()
close_web_driver()
来源:https://blog.csdn.net/guduyishuai/article/details/78988793
0
投稿
猜你喜欢
- 1. 多线程的作用简而言之,多线程是并行处理相互独立的子任务,从而大幅度提高整个任务的效率。2. Python中的多线程相关模块和方法Pyt
- 这是第二天了,工作之余和女朋友一起学Python3,代码都是她敲的,有点辣眼睛,仅做参考。1.题目:输入“姓名”,输出“你好,姓名”有关安装
- 著名的老掉牙的IE6.0在我这里已经有六年工龄了,前几天朋友拿到个IE8.0新的Beta版本,我的Sever2003装不上,大为扫兴。Chr
- 本文实例讲述了PHP读取txt文本文件并分页显示的方法。分享给大家供大家参考。具体实现方法如下:<?php &n
- JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的。而cookie是运行在
- 本文实例讲述了Python基于回溯法子集树模板解决野人与传教士问题。分享给大家供大家参考,具体如下:问题在河的左岸有N个传教士、N个野人和一
- 本文为大家分享了threadpool线程池中所有的操作,供大家参考,具体内容如下首先介绍一下自己使用到的名词:工作线程(worker):创建
- 本文实例讲述了Python设计模式之命令模式原理与用法。分享给大家供大家参考,具体如下:命令模式(Command Pattern):将请求封
- //定义编码header( 'Content-Type:text/html;charset=utf-8 ');//Atomh
- TNS简要介绍与应用 Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它
- Dreaweaver MX 2004 中增加了图片处理功能,如图片亮度和对比度的调节、图片的锐化效果等
- python脚本性能分析首先使用cd进入需要测试的脚本文件对应的目录,然后再使用如下代码完成对脚本的性能测试。# enter the dir
- 本文实例讲述了Python使用matplotlib 模块scatter方法画散点图。分享给大家供大家参考,具体如下:# -*-coding:
- 源代码如下:#-*- coding:utf-8 -*- def check_exsit(process_name): import win3
- 本文实例讲述了Python设计模式之MVC模式。分享给大家供大家参考,具体如下:一.简单介绍mvc模式 the mo
- asp时间加减运算 和转换问题 a=2007-07-24 2:23:15 b=2005-06-25 2:23:15 问题1 如何将a转换成2
- ps:不曾想还有那么好用的方法。汗一个先。Div即父容器不根据内容自适应高度,我们看下面的代码:<div id="main&
- Python 通过pip安装Django详细介绍经过前面的 Python 包管理工具的学习,接下来我们就要基于前面的知识,来配置 Djang
- 在命令行中使用 Python 时,它可以接收大约 20 个选项(option),语法格式如下:python [-bBdEhiIOqsSuvV
- 目前为止,我们使用函数时所用的参数都是位置参数,即传入函数的实际参数必须与形式参数的数量和位置对应。而本节将介绍的关键字参数,则可以避免牢记