selenium抓取元素排除某个特定的class标签

排除某个因素,第一优选想到正则表达式,无奈折腾半天没有成功,感觉是selenium对元素的attrs按re search在操作,$对字符串末尾检测都没什么用。

BeautifulSoup可以用element[‘class’]输出元素的class进行检测,但是BeautifulSoup对象不能再进行click操作,不符合期望。selenium没有这样的语法,通过xpath进行选择:

itemList = driver.find_elements_by_xpath(‘//div[@id = "choose-color"]//div[@class = "dd"]//div[not(contains(@class, "disabled"))]‘)

stackoverflow有个参考:http://stackoverflow.com/questions/11024080/how-to-use-not-contains-in-xpath

补充xpath用法:

xpath=xpathExpression: Locate an element using an XPath expression.  
  
xpath=//img[@alt=’The image alt text’]  
xpath=//table[@id=’table1’]//tr[4]/td[2]  
xpath=//a[contains(@href,’#id1’)]  
xpath=//a[contains(@href,’#id1’)]/@class  
xpath=(//table[@class=’stylee’])//th[text()=’theHeaderText’]/../td  
xpath=//input[@name=’name2’ and @value=’yes’]  
xpath=//*[text()=”right”]
时间: 2024-08-29 17:46:18

selenium抓取元素排除某个特定的class标签的相关文章

selenium抓取淘宝数据报错:warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless

ssh://[email protected]:22/root/anaconda3/bin/python3 -u /www/python3/maoyantop100/meishi_selenium.py /root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been depre

用Selenium抓取新浪天气

(1)用Selenium抓取新浪天气 系统环境: 操作系统:macOS 10.13.6 python :2.7.10 用虚拟环境实现 一.创建虚拟环境: mkvirtualenv --python=/usr/bin/python python_2 二.激活虚拟环境: workon python_2 三.安装Selenium pip install Selenium 四.安装firefox的Selenium补丁文件: brew install geckodriver 五.在~/.bash_prof

用正则表达式抓取网页中的ul 和 li标签中最终的值!

获取你要抓取的页面 const string URL = "http://www.hn3ddf.gov.cn/price/GetList.html?pageno=1";            string htmlStr = null;            for (int i = 0; i < 10; i++)            {                try                {                    System.Net.Http

使用selenium抓取JS动态生成的页面

在抓取网页数据时,传统jsoup方案只能对静态页面有效,而有些网页数据往往是js生成的,所以这时候需要其它的方案. 首先的思路是分析js程序,对js的请求进行再次抓取,这适合于特定的页面抓取,要做到对不同目标URL的通用性,比较麻烦. 第二种思路,也是比较成熟的做法是利用第三方的驱动渲染页面,然后下载.这里介绍一下第二种实现思路. Selenium是一个模拟浏览器的自动化测试工具,它提供一组API可以与真实的浏览器内核交互. Java环境下的maven配置如下: <dependency> &l

[Python爬虫] 之四:Selenium 抓取微博数据

抓取代码: # coding=utf-8import osimport refrom selenium import webdriverimport selenium.webdriver.support.ui as uifrom selenium.webdriver.common.keys import Keysimport timefrom selenium.webdriver.common.action_chains import ActionChainsimport IniFileclas

用python+selenium抓取豆瓣电影中的正在热映前12部电影并按评分排序

抓取豆瓣电影(http://movie.douban.com/nowplaying/chengdu/)中的正在热映前12部电影,并按照评分排序,保存至txt文件 #coding=utf-8 from selenium import webdriver import unittest from time import sleep class DoubanMovie(unittest.TestCase):     def setUp(self):          self.dr = webdriv

用python+selenium抓取微博24小时热门话题的前15个并保存到txt中

抓取微博24小时热门话题的前15个,抓取的内容请保存至txt文件中,需要抓取阅读数 #coding=utf-8 from selenium import webdriver import unittest from time import sleep class Weibo(unittest.TestCase): def setUp(self): self.dr = webdriver.Chrome() self.hot_list = self.get_weibo_hot_topic() sel

用python+selenium抓取豆瓣读书中最受关注图书,按照评分排序

抓取豆瓣读书中的(http://book.douban.com/)最受关注图书,按照评分排序,并保存至txt文件中,需要抓取书籍的名称,作者,评分,体裁和一句话评论 #coding=utf-8 from selenium import webdriver from time import sleep class DoubanPopularBook:     def __init__(self):         self.dr = webdriver.Chrome()         self.

用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件

抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答,保存至html文件,该html文件的文件名应该是20160228_zhihu_today_hot.html,也就是日期+zhihu_today_hot.html 代码如下: from selenium import webdriver from time import sleep import time class ZhiHu():    def __init__(self):       self.dr = webdriver.Chr