今天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. Python3+HTMLTestRunner+SMTP生成测试报告后发送邮件

    在前一篇https://www.cnblogs.com/zhengyihan1216/p/11549820.html 中记录了如何生成html格式的报告, 这篇记录下怎么将测试报告通过邮件发出 1.对 ...

  2. computer networking ---------DNS

    [DNS]domain named system 域名解析系统,即相当于对www.baidu.com的类似的域名进行解析,对于人而言,记忆一些域名相比于记忆一些Ip地址来说简单的多,而对于计算机而言, ...

  3. Java精通并发-Condition详解及相比于传统线程并发模式的改进

    在上一次https://www.cnblogs.com/webor2006/p/11792954.html对于Lock的具体实现类ReentrantLock用了一个示例对它进行了一个简单的了解,而它其 ...

  4. adb命令操作蓝牙

    打开和关闭蓝牙BT adb root adb shell svc bluetooth enable adb shell svc bluetooth disable UI层 查询:adb shell s ...

  5. 从0到1的开发,社交App 完成

    内容概要 GitHub链接:GitHub链接 客户端使用Android Studio 服务端使用IDEA + SpringBoot + MyBaits 完成功能 添加好友,即时聊天,社交广场 只是一个 ...

  6. 14-C#笔记-字符串

    1. 基本操作 using System; namespace StringApplication { class Program { static void Main(string[] args) ...

  7. mysql之drop、truncate和delete的区别

    今天在整理mysql数据库笔记的时候突然想到一个问题,就是drop.truncate和delete的区别,乍一看三者都是有删除的功能,但是具体来看还是有很多区别的.我先把这三个的作用简单说一下,有前辈 ...

  8. 树莓派项目(1-3 )目标识别 NNPACK支持版Darknet,可用于树莓派等嵌入设备

    https://github.com/digitalbrain79/darknet-nnpack https://github.com/AlexeyAB/darknet#how-to-train-to ...

  9. q1095

    一,写题 1,我这个递归的错误我挺想搞出来的 int fa(int x) { ) return cnt; ==) { x=x/; cout<<"测试1:"<< ...

  10. ESA2GJK1DH1K升级篇: 移植远程更新程序到STM32F103RET6型号的单片机,基于(GPRS模块AT指令TCP透传方式)

    前言 上节实现远程更新是更新的STM32F103C8T6的单片机 GPRS网络(Air202/SIM800)升级STM32: 测试STM32远程乒乓升级,基于(GPRS模块AT指令TCP透传方式),定 ...