今天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. 浅谈MongoDB基础及架构

    1.简述MongDB是一个强大.灵活而可扩展的数据存储系统,其将强大的可扩展特性与关系库最有用的特性进行了整合,像:次级索引,范围查询和排序等特性.而MongDB也内建了类似MapReduce汇聚和地 ...

  2. Oracle 11.2.0.4单实例打补丁

    Oracle 11.2.0.4单实例打PSU,OJVM PSU补丁快速参考 写在前面: ·         1.Oracel打每个补丁的操作有时存在差异,所以不管多熟悉,都应该在打任何补丁之前阅读新补 ...

  3. MYSQL性能测试工具SYSBENCH

    [root@localhost ~]$ wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbench-1.0. ...

  4. AIX运维常用命令

    目前传统的磁盘管理仍有不足:如果下Unix系统中的存储容量需要扩展,文件系统就必须停止运行,然后通过重构分区的手段来进行分区和文件系统的扩容.一般采用的方法是先备份该文件系统并删除其所在的分区,然后重 ...

  5. cookie跨域解决方案

    cookie的名/值对中的值不允许出现分号.逗号和空白符,因此在设置cookie前要用encodeURIComponent()编码,读取时再用decodeURIComponent()解码. cooki ...

  6. 修改ceph的IP地址

    目录 修改步骤如下 1.导出mon配置 2.修改mon配置 3.修改ceph的配置文件 4.关闭ceph集群(建议将集群内的所有节点中的ceph都关闭) 5.修改服务器IP 6.导入修改后的mon 7 ...

  7. metrics-server 安装问题解决

    参考:  https://www.qikqiak.com/post/install-metrics-server/   git clone https://github.com/kubernetes- ...

  8. 编译3516cv500 osdrv失败解决

    configure: WARNING: cannot find uuid library required for mkfs.ubifsconfigure: mtd-utils can optiona ...

  9. maven 配置参数详解

    引自:搬砖工的奋斗史www.cnblogs.com/laobiao/p/5589025.html <project xmlns="http://maven.apache.org/POM ...

  10. CMD窗口恢复默认设置

    CMD全称Command,是Windows系统下自带的类DOS系统,在日常工作中,有时候设置会损害CMD窗口的默认,导致浏览效果不佳,这时候需要有办法恢复到默认设置.在注册表中删除以下文件夹即可:HK ...