本文转载:http://blog.csdn.net/u011541946/article/details/68485981

练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操作。但是这些字段可能在一个网页的不同地方。例如,我们需要在关于百度页面-联系我们,摘取全部的邮箱。

思路拆分:

1. 首先,需要得到当前页面的source内容,就像,打开一个页面,右键-查看页面源代码。

2. 找出规律,通过正则表达式去摘取匹配的字段,存储到一个字典或者列表。

3. 循环打印字典或列表中内容,Python中用 for 语句实现。

技术角度实现相关方法:

1. 查看页面的源代码,在Selenium中有driver.page_source 这个方法得到

2. Python中利用正则,需要导入re模块

3. for email in emails :

print email

 # coding=utf-8  

 from selenium import webdriver
import re driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6) driver.get("http://home.baidu.com/contact.html")
# 得到页面源代码
doc = driver.page_source
emails = re.findall(r'[\w]+@[\w\.-]+',doc) # 利用正则,找出 xxx@xxx.xxx 的字段,保存到emails列表
# 循环打印匹配的邮箱
for email in emails:
print (email)

解释:

在python正则表达式语法中,Python中字符串前面加上 r 表示原生字符串,用\w表示匹配字母数字及下划线。re模块下findall方法返回的是一个匹配子字符串的列表。

Python+Selenium之摘取网页上全部邮箱的更多相关文章

  1. Python+selenium点击网页上指定坐标

    from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains dr = ...

  2. Python+Selenium练习篇之1-摘取网页上全部邮箱

    前面已经介绍了Python+Selenium基础篇,通过前面几篇文章的介绍和练习,Selenium+Python的webUI自动化测试算是入门了.接下来,我计划写第二个系列:练习篇,通过一些练习,了解 ...

  3. Python+ Selenium自动化登录腾讯QQ邮箱实例

    学习了Python语言一段时间后,在公司的项目里也使用到了python来写测试脚本,一些重复的操作都使用脚本来处理了.大大的提高工作效率,减少了一些手工重复的操作. 以下是使用unittest框架写的 ...

  4. Python + Selenium 练习篇 - 获取页面所有邮箱

    代码如下: # coding=utf-8import re    #python中利用正则,需要导入re模块from selenium import webdriverdriver = webdriv ...

  5. Python+selenium(Autolt实现上传)

    AutoIt是一个使用类似BASIC脚本语言的免费软件,被设计用来进行Windows GUI的自动化测试.它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务. 此次小编介绍的是利用Auto ...

  6. python+selenium+autoit实现文件上传

    问题 在做web端ui层自动化的时候会碰到文件上传的操作,经常有朋友问到,这里总结一下 解决方案 第一种:type=file的上传文件,类似如下的 使用类似这样的代码就可以完成: driver.fin ...

  7. python+selenium win32gui实现文件上传 enumerate()

    upload = dr.find_element_by_id('exampleInputFile0') upload.click() time.sleep(1) # win32gui dialog = ...

  8. python selenium +autoit实现文件上传 --实践

    upload.html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...

  9. python selenium等待特定网页元素加载完毕

    selenium等待特定元素加载完毕 is_disappeared = WebDriverWait(driver, 8, 0.5, ignored_exceptions=TimeoutExceptio ...

随机推荐

  1. SSAS IIS 发布

    http://www.cnblogs.com/zhangzt/p/4046259.html IIS7下配置SSAS通过HTTP远程连接 淘宝 问答 学院 博客 资源下载 高端培训            ...

  2. 面试总结hashmap

    考点: 1.hashing的概念 2.HashMap中解决碰撞的方法 3.equals()和hashCode()的应用,以及它们在HashMap中的重要性 4.不可变对象的好处 5.HashMap多线 ...

  3. c的scanf为什么比c++的cin快

    很早就知道,c的scanf(printf)比c++的快.刷题时尤其明显,在这上面超时是常有的事儿. 但,这是别人告诉我的,c快. 为什么快? 从网上借鉴一个例子做个简单测试: 1.cpp     // ...

  4. Python3 编译中文字串报错解决方案

    问题: Python3.6.5 版本中,程序有中文,运行时出现以下error: SyntaxError: Non-UTF-8 code starting with '\xb2' in file XXX ...

  5. Windows Error Codes

    http://www.briandunning.com/error-codes/?source=Windows Windows Error Codes List All Error Codes | S ...

  6. LeetCode: 371 Sum of Two Integers(easy)

    题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  7. Vue拖拽组件列表实现动态页面配置

    需求描述 最近在做一个后台系统,有一个功能产品需求是页面分为左右两部分,通过右边的组件列表来动态配置左边的页面视图,并且左边由组件拼装起来的视图,可以实现上下拖拽改变顺序,也可以删除. 根据这个需求我 ...

  8. 327. Count of Range Sum(inplace_marge)

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. UE4 c++ 创建刚体Cube

    1 新建一个Actor,一会用蓝图继承这个 TCubeActor.h #pragma once #include "CoreMinimal.h" #include "Ga ...

  10. React入门看这篇就够了

    摘要: 很多值得了解的细节. 原文:React入门看这篇就够了 作者:Random Fundebug经授权转载,版权归原作者所有. React 背景介绍 React 入门实例教程 React 起源于 ...