今天seo的同事需要一个简单的爬虫工具, 根据一个url地址,抓取改页面的a连接,然后进入a连接里面的页面再次抓取a连接

1.需要一个全局的set([])集合来保存抓取的url地址

2.由于现在单页面也来越多,所以我们借用selenium来抓取页面内容, 由于页面内容比较多, 我们程序需要将滚动条滚到最下面,如:driver.execute_script("return document.body.scrollHeight;")

3.需要查找页面的超链接 driver.find_elements_by_xpath("//a[@href]")

4.为了便于查看数据记录,每抓取一个地址就记录到日志中去(曾经尝试过爬网完毕后再记录,但是爬网时间太长,一旦出现异常就一条记录都没有了)

整个代码如下:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.common.exceptions import TimeoutException
import time
import datetime
from urllib import parse
import os urls = set([])
def getUrl(url,host):
driver = webdriver.Ie()
try:
#driver = webdriver.Firefox()
driver.set_page_load_timeout()
driver.get(url)
#time.sleep() all_window_height = []
all_window_height.append(driver.execute_script("return document.body.scrollHeight;"))
while True:
driver.execute_script("scroll(0,100000)")
time.sleep()
check_height = driver.execute_script("return document.body.scrollHeight;")
if check_height == all_window_height[-]:
print("我已下拉完毕")
break
else:
all_window_height.append(check_height)
print("我正在下拉") #for link in driver.find_elements_by_xpath("//*[@href]"):
#for link in driver.find_elements_by_tag_name("a"):
for link in driver.find_elements_by_xpath("//a[@href]"):
try:
tempurl1=link.get_attribute('href')
if tempurl1.startswith("http"):
if tempurl1 not in urls:
urls.add(tempurl1)
log(host,url+','+tempurl1)
print(tempurl1)
except:
print(link)
except Exception as e:
print(e)
finally:
driver.quit() def log(name,msg):
filename='D://'+name+'.csv'
if not os.path.exists(filename):
with open(filename,'w') as f:
print('create file:'+filename)
f.write('parentUrl,currenturl'+'\n')
f.close()
with open(filename,'a') as f:
f.write(msg+'\n')
f.close() url= input("Enter a url")
try:
urls.clear()
url= url.strip()
if len(url)>:
host =parse.urlparse(url).netloc
print(url+"下面的连接:")
t1=datetime.datetime.now()
getUrl(url,host)
l=list(urls)
for item in l:
print(item+"下面的连接:")
getUrl(item,host)
t2=datetime.datetime.now()
tt =(t2-t1).seconds
minutes=tt//
seconds=tt%
print("total cost %d minutes %d seconds" % (minutes,seconds)) except Exception as e:
print(e)

然后运行pyinstaller -F a.py 打包

关于selenium 的IE 可以参考https://blog.csdn.net/ma_jiang/article/details/96022775

python selenium爬虫工具的更多相关文章

  1. Python selenium爬虫实现定时任务过程解析

    现在需要启动一个selenium的爬虫,使用火狐驱动+多线程,大家都明白的,现在电脑管家显示CPU占用率20%,启动selenium后不停的开启浏览器+多线程, 好,没过5分钟,CPU占用率直接拉到9 ...

  2. Python+Selenium爬虫实战一《将QQ今日话题发布到个人博客》

    前提条件: 1.使用Wamp Server部署WordPress个人博客,网上资料较多,这里不过多介绍 思路: 1.首先qq.com首页获取到今日话题的的链接: 2.通过今日话题链接访问到今日话题,并 ...

  3. Python 爬虫实例(12)—— python selenium 爬虫

    # coding:utf- from common.contest import * def spider(): url = "http://www.salamoyua.com/es/sub ...

  4. python爬虫工具

    一直都听说python写爬虫工具非常方便,为了获取数据,我也要写点爬虫,但是python太灵活了,不知道python爬虫要哪些框架,要了解,比如beatiful soup,scrapy, 爬虫的额主要 ...

  5. Python selenium自动化网页抓取器

    (开开心心每一天~ ---虫瘾师) 直接入正题---Python selenium自动控制浏览器对网页的数据进行抓取,其中包含按钮点击.跳转页面.搜索框的输入.页面的价值数据存储.mongodb自动i ...

  6. Python selenium 滚动条 详解

    在我们使用Python + selenium 爬虫的时候,会遇到如下报错,原因是  当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的. selenium.common ...

  7. Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)

    原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开 ...

  8. 【Python】Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱

    本文转载自:https://www.cnblogs.com/colipso/p/4284510.html 好文 mark http://www.52nlp.cn/python-%E7%BD%91%E9 ...

  9. python网络爬虫之自动化测试工具selenium[二]

    目录 前言 一.获取今日头条的评论信息(request请求获取json) 1.分析数据 2.获取数据 二.获取今日头条的评论信息(selenium请求获取) 1.分析数据 2.获取数据 房源案例(仅供 ...

随机推荐

  1. js正则只能包含小写数字分割符,切不能以分割符开头和结尾

    const version = /^(?!_)(?!.*-$)[a-z0-9_]+$/; 1.一个正则表达式,只含有数字.小写字母.中划线不能以中划线开头和结尾: ^(?!-)(?!.*-$)[a-z ...

  2. window配合虚拟机VMware搭建虚拟ubuntu服务器入坑集锦

    1.VMware虚拟机和主机进行网络连接设置 https://jingyan.baidu.com/article/adc81513b86621f723bf7383.html 2.解决linux虚拟机与 ...

  3. TP框架命令行操作

    cmd进入到tp项目根目录 php think route:list #查看以定义路由 php think version #框架版本 php think list #指令列表 php think h ...

  4. Docker在Centos 7上的部署

      Docker在Centos 7上的部署 方法1---开启centos 7上自带的 extras YUM源,然后 yum install docker来安装 安装前必须保证 Linux Kernel ...

  5. centos7利用acme.sh获取Let's Encrypt的永久免费ssl证书并配置网站域名https访问

    acme.sh介绍:https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E github:https://github.com/Neil ...

  6. Linux(Centos7)安装Oracle11.2.0数据字典初始化,监听,网络,创建用户等部分配置

    #创建数据字典和pl/sql包 @/u01/app/oracle/product/11.2.0/db_1/rdbms/admin/catalog.sql; @/u01/app/oracle/produ ...

  7. 可变lambda, lambda使用mutable关键字

    关于lambda的捕获和调用 C++ primer上对可变lambda举的例子如下: size_t v1=42; auto f=[v1] () mutable{return ++v1; }; v1=0 ...

  8. Java多线程编程核心技术-第3章-线程间通信-读书笔记

    第 3 章 线程间通信 线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体.线程间的通信就是成为整体的必用方案之一,可以说,使线程间进行通信后,系统之间的交互性会更强大,在大 ...

  9. Python学习笔记8 pip换源

    换pip源 安装好 python 后,有一个问题就是 pip 安装的默认下载地址在国外.要换一下,不然蜗牛速度,怎么追赶世界?(瞎扯一下,活跃气氛~) Ubuntu下,进入终端(ctrl + alt ...

  10. el获取对象属性大小写问题

    当el表达式首字母大写,会判断第二个字母大小写,小写,抛异常:大写,则直接拼接get,形成getter方法 当el表达式首字母小写,会判断第二个字母大小写,大写,必须第一个字母大写:否则无法获取值 e ...