xpath

How to search for a Xml node with a given attribute value in python

一曲冷凌霜 提交于 2020-01-30 06:22:30
问题 I have this XML file and I want to get the country nodes which have the pattern 'in' in their name. <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011<

How to search for a Xml node with a given attribute value in python

有些话、适合烂在心里 提交于 2020-01-30 06:20:06
问题 I have this XML file and I want to get the country nodes which have the pattern 'in' in their name. <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011<

拉钩爬虫

本小妞迷上赌 提交于 2020-01-29 22:34:41
#!/usr/bin/env python# -*- coding:utf-8 -*-import jsonimport reimport timeimport lxml.htmlfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom redis_cache import RedisCacheclass LagouSpider(object): def __init__(self): # 调用webdriver.Chrome()启动浏览器 self.driver = webdriver.Chrome() self.url = 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=' self.detail_url = None def run(self): # 获得url打开浏览器 self.driver.get(self.url) while

Python操作APP -- Appium-Python-Client

吃可爱长大的小学妹 提交于 2020-01-29 11:38:13
pip install Appium-Python-Client 使用Appium定位或者使用辅助定位工具 SDK安装目录/tools/bin,双击此辅助定位工具 from appium import webdriver # 配置信息 desired = { "platformName": "Android", "platformVersion": "5.1.1", "deviceName": "127.0.0.1:62026", "appPackage": "com.android.settings", "appActivity": ".Settings" } # 固定路径 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired) # 获取屏幕宽高 width, height = driver.get_window_size().values() # 滚屏 耗时800ms driver.swipe(width * 0.5, height * 0.8, width * 0.5, height * 0.3, 800) # 定位元素 # ID # element = driver.find_element_by_id('com.android.settings:id/title') # xpath ele

Jsonpath的写法

…衆ロ難τιáo~ 提交于 2020-01-29 04:39:01
JSONPath - 是xpath在json的应用。 xml最大的优点就有大量的工具可以分析,转换,和选择性的提取文档中的数据。XPath是这些最强大的工具之一。 如果可以使用xpath来解析json,以下的问题可以被解决: 1,数据不使用特殊的脚本,可以在客户端交互的发现并取并获取。 2,客户机请求的JSON数据可以减少到服务器上的相关部分,这样可以最大限度地减少服务器响应的带宽使用率。 如果我们愿意,这个可以解析json数据的工具会变得有意义。随之而来的问题是它如何工作,jsonpath的表达式看起来怎么样。 事实上,json是由c系统编程语言表示自然数据,有特定语言的特定语法来访问json数据。 xpath的表达式: /store/book[1]/title 我们可以看作是: x.store.book[0].title 或 x['store']['book'][0]['title'] 在Javascript, Python 和 PHP 中一个变量x表示json数据。经过观察,特定的语言里有内置xpath来解析数据。 JSONPath工具的问题 -依赖某种特定的语言 - 需要依赖XPath 1.0 - 减少代码量和内存的消耗 - 在运行时 JSONPath 表达式 JSONPath 是参照,xpath表达式来解析xml文档的方式

How does dot(.) in xpath to take multiple form in identifying an element and matching a text

徘徊边缘 提交于 2020-01-28 12:35:03
问题 I have the below dom structure: <h3 class="popover-title"> <div class="popup-title"> <div class="title-txt">Associated Elements &nbsp(5)</div> </div> </h3> I am trying to write an xpath which will identify the title "Associated Elements" under h3 tag. When my xpath is //div[contains(@class, popover)]//h3[contains(.,'Associated Elements')] the element is identified. However when my xpath is //div[contains(@class, popover)]//h3[contains(text(),'Associated Elements')] the element is not

How does dot(.) in xpath to take multiple form in identifying an element and matching a text

懵懂的女人 提交于 2020-01-28 12:31:51
问题 I have the below dom structure: <h3 class="popover-title"> <div class="popup-title"> <div class="title-txt">Associated Elements &nbsp(5)</div> </div> </h3> I am trying to write an xpath which will identify the title "Associated Elements" under h3 tag. When my xpath is //div[contains(@class, popover)]//h3[contains(.,'Associated Elements')] the element is identified. However when my xpath is //div[contains(@class, popover)]//h3[contains(text(),'Associated Elements')] the element is not

爬虫:Scrapy6 - Item Loaders

喜欢而已 提交于 2020-01-27 20:17:17
Item Loaders 提供了一种便捷的方式填充抓取到的:Items。虽然 Items 可以使用自带的类字典形式的 API 填充,但是 Item Loaders 提供了更便捷的 API,可以分析原始数据并对 Item 进行填充。 Items 提供保存抓取数据的容器,而 Item Loaders 提供的是填充容器的机制。 Item Loaders 提供的是一种灵活,高效的机制,可以更方便的被 spider 或 source format (HTML,XML,etc)扩展,并 override 更易于维护的、不同的内容分析规则。 Using Item Loaders to populate items 要使用 Item Loaders,必须先将它实例化。可以使用类似字典的对象(例如:Item or dict)来进行实例化,或者不使用对象也可以。当不用对象进行实例化的时候,Item 会自动使用 ItemLoader.default_item_class 属性中指定的 Item 类在 Item Loader constructor 中实例化。 然后,开始收集数值到 Item Loader 时,通常使用 Selectors。可以在同一个 item field 里面添加多个数据;Item Loader 将指导如何用合适的处理函数来“添加”这些数值。 下面是在 Spider 中典型的 Item

爬虫之scrapy框架

本小妞迷上赌 提交于 2020-01-27 13:07:56
一 scrapy框架简介 1 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速、简单、可扩展的方式从网站中提取所需的数据。但目前Scrapy的用途十分广泛,可用于如数据挖掘、监测和自动化测试等领域,也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫。Scrapy 是基于twisted框架开发而来,twisted是一个流行的事件驱动的python网络框架。因此Scrapy使用了一种非阻塞(又名异步)的代码来实现并发。 整体架构大致如下: """ Components: 1、引擎(EGINE) 引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。有关详细信息,请参见上面的数据流部分。 2、调度器(SCHEDULER) 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址 3、下载器(DOWLOADER) 用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的 4、爬虫(SPIDERS) SPIDERS是开发人员自定义的类,用来解析responses,并且提取items

【Python数据采集】提取页面内容的几种手段

我与影子孤独终老i 提交于 2020-01-27 04:03:40
前言 在我们获取了网页的信息后,往往需要对原始信息进行提取,得到我们想要的数据。对信息的提取方式主要有以下几种:正则表达式、XPath、BeautifulSoup。本篇博客主要总结这三种方式的基本语法,以及举一些例子来说明如何使用这些方法。 正则表达式 什么是正则表达式? 正则表达式是使用某种预定义的模式去匹配一类具有共同特征的字符串,主要用于处理字符串,可以快速、准确地完成复杂的查找、替换等要求。 在Python中, re模块 提供了正则表达式操作所需要的功能。所以,在Python中使用正则表达式需要先 import re 。 在使用正则表达式提取信息时可以概括为以下三步(大部分提取信息方法的步骤也是如此): 寻找规律 使用正则符号表示规律 提取信息 正则表达式的基本符号 这里主要介绍正则中的基本符号,高级的语法的部分会在后面附上链接供大家参考学习。 一般符号 名称 描述 示例 点号. 匹配除换行符 \n 以外任意单个字符,若是要匹配 . 则需要使用转义字符 \ a.c -> a b c, a # c 方括号[] 字符集(字符类)。对应的位置可以是指定字符集中的任意字符,[]中的字符可以逐个列出,也可以给出范围。^符号表示取反,即除指定字符以外的其他字符。 a[bcd]e -> a b e; a[b-f]g -> a b g; a[^bc]d -> aefd