今天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. 微信小程序开发--常用开发实例

    一.常用商品列表的换行排布 <view class="box_max"> <view class="box_min">限时秒杀</ ...

  2. 监控进程cpu meminfo

    https://github.com/cdrandin/cpsc_351 https://github.com/cdrandin?after=Y3Vyc29yOnYyOpK5MjAxNC0wNy0xM ...

  3. LNMP+memcached

    部署LNMP+memcached网站平台,通过PHP页面实现对memcached服务器的数据操作,实现以下目标:部署LNMP实现PHP动态网站架构为PHP安装memcache扩展创建PHP页面,并编写 ...

  4. LeetCode 1213. Intersection of Three Sorted Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-three-sorted-arrays/ 题目: Given three integer a ...

  5. ESA2GJK1DH1K基础篇: 阿里云物联网平台: 测试云平台显示MQTT客户端发过来的消息

    现在这里空空如也 咱自定义的也没有数据 现在就是传上来温度数据,让这里显示温度数据 你发布的主题  /sys/a1m7er1nJbQ/Mqtt/thing/event/property/post 发布 ...

  6. 1. Spring Cloud Greenwich SR2 概览

    Spring Cloud provides tools for developers to quickly build some of the common patterns in distribut ...

  7. 请指出document load和document ready的区别

    document load文档的所有内容都加载完成 document ready文档的DOM加载完成

  8. 超强在线考试系统源码(私有部署&二次开发)

    随着信息化技术的发展,考试系统也在进行着深入的变革.从传统的纸质考试人工评分到现在的在线考试自动评分. 在线考试系统的应用场景也在逐渐扩宽,例如:学校的学生考试.员工培训考试.招聘考试.职称考试等等. ...

  9. 实例解读什么是Redis缓存穿透、缓存雪崩和缓存击穿

    from:https://baijiahao.baidu.com/s?id=1619572269435584821&wfr=spider&for=pc Redis缓存的使用,极大的提升 ...

  10. SpringMVC 字节流实现播放多媒体

    1.前言 在项目中,我们会遇到在线预览,播放MP3.图片.MP4等.用户上传文件后,将路径存储在数据库中,我们可动态读取数据库的数据,然后通过返回文件路径的字符串,在src中发送请求.当然这需要带参数 ...