lxml

XPath variables in lxml

余生颓废 提交于 2020-02-02 15:33:09
问题 I want to use XPath variables to match a user-defined tag and avoid XPath injection vulnerabilities. I have tried from lxml import etree etree.fromstring('<div><p>Hello</p></div>').xpath('.//$var', var='p') but I get XPathEvalError: Invalid expression What am I doing wrong? 回答1: You cannot use a variable as the node test part of a location step in an expression. It has to be a literal name. But you can use a wildcard and a predicate. The following works: etree.fromstring('<div><p>Hello</p><

XPath variables in lxml

拥有回忆 提交于 2020-02-02 15:32:01
问题 I want to use XPath variables to match a user-defined tag and avoid XPath injection vulnerabilities. I have tried from lxml import etree etree.fromstring('<div><p>Hello</p></div>').xpath('.//$var', var='p') but I get XPathEvalError: Invalid expression What am I doing wrong? 回答1: You cannot use a variable as the node test part of a location step in an expression. It has to be a literal name. But you can use a wildcard and a predicate. The following works: etree.fromstring('<div><p>Hello</p><

一个简单的接口测试案例

六眼飞鱼酱① 提交于 2020-02-01 20:50:51
比如,我们需要实现百度某一只股票,查询出股票的当前价格和市值 我们查询”600754“这只股票的当前价格和市值 实现步骤如下: 1、导入request库,还需要额外导入lxml库(后面使用的到) import requests #倒入requests库 from lxml import etree #倒入lxml 库(没有这个库,pip install lxml安装) 2、构造请求数据,百度搜索发起请求的数据比较多 cookies = { 'BIDUPSID': '90EF3BD78F53BC8C96DF84CD3854CA2D', 'PSTM': '1578233930', 'BD_UPN': '12314753', 'BAIDUID': '885754C8E6BD7B1A771802631815CC6D:FG=1', 'BDORZ': 'B490B5EBF6F3CD402E515D22BCDA1598', 'BDUSS': 'mxYdVpwOEx0eGJsT3VUYTJXbkZJYWhKSGpQWnlqaVBwMlExTWNNRkR4cWtabHRlSVFBQUFBJCQAAAAAAAAAAAEAAACRJsY

python3安装lxml(windows)

百般思念 提交于 2020-01-31 05:26:28
爬虫时通常要安装LXML,对于通过一下命令行 1 pip install lxml 出现如下错误的解决方法 1 lxml Unable to find vcvarsall.bat 1. 安装wheel,命令行运行: 1 pip install wheel 2.在这里下载对应的.whl文件(打开网址后ctrl+F,搜索LXML,选择对应版本,一般是win32,而不选win_arm64,即使电脑是win64) http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 3. 进入.whl所在的文件夹->shift+鼠标右键->在此处打开命令窗口 4.执行命令即可完成安装: 1 pip install lxml-3.6.0-cp35-cp35m-win32.whl 小北 2016/3/23 来源: https://www.cnblogs.com/ityoung/p/5309799.html

pip 安装 lxml 出错

折月煮酒 提交于 2020-01-31 04:02:54
  用pip安装 lxml 老是出错,在公司安装了 wheel,从 http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下载了lxml的whl包,pip insall lxml 就可以了。 在家里如法炮制,结果老是提示 lxml-3.6.4-cp34-cp34m-win32.whl is not a supported wheel on this platform , 试了64位包和32包都不行,没办法了, 只能手动安装,将下载的whl包重命名 lxml-3.6.4-cp34-cp34m-win32.rar 解压得到 lxml文件夹,拷贝放入Python34\Lib\site-packages 即可,搞定,收工。 来源: https://www.cnblogs.com/yemeng/p/6075597.html

django高级之爬虫基础

会有一股神秘感。 提交于 2020-01-30 06:23:51
目录: 爬虫原理 requests模块 beautifulsoup模块 爬虫自动登陆示例 一、爬虫原理 Python非常适合用来开发网页爬虫,理由如下: 1、抓取网页本身的接口 相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁;相比其他动态脚本语言,如perl,shell,python的urllib包提供了较为完整的访问网页文档的API。(当然ruby也是很好的选择) 此外,抓取网页有时候需要模拟浏览器的行为,很多网站对于生硬的爬虫抓取都是封杀的。这是我们需要模拟user agent的行为构造合适的请求,譬如模拟用户登陆、模拟session/cookie的存储和设置。在python里都有非常优秀的第三方包帮你搞定,如Requests,mechanize 2、网页抓取后的处理 抓取的网页通常需要处理,比如过滤html标签,提取文本等。python的beautifulsoap提供了简洁的文档处理功能,能用极短的代码完成大部分文档的处理。 其实以上功能很多语言和工具都能做,但是用python能够干得最快,最干净。 3、爬虫架构 URL管理器:管理待爬取的url集合和已爬取的url集合,传送待爬取的url给网页下载器。 网页下载器(urllib、requests):爬取url对应的网页,存储成字符串或文件,传送给网页解析器。 网页解析器

BeautifulSoup解析库

大兔子大兔子 提交于 2020-01-30 02:33:23
解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(html, 'html.parser') 速度适中,容错能力强 老版本python容错能力差 lxml HTML解析库 BeautifulSoup(html, 'lxml') 速度快,容错能力强 安装c语言库 lxml XML解析库 BeautifulSoup(html, 'xml') 速度快,唯一支持XML的解析器 安装c语言库 html5lib BeautifulSoup(html, 'html5lib') 最高的容错性,浏览器方式解析文档,生成HTML格式文档 速度慢 基本使用 from bs4 import BeautifulSoup html = ''' <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,

BeautifulSoup

早过忘川 提交于 2020-01-30 02:28:20
一 什么是BeautifulSoup 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。 官方解释如下:   Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。   Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库。它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。所以需要配合解析器一起使用!   Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4。 解析器:   Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐安装另一个可供选择的解析器是纯Python实现的 html5lib , html5lib的解析方式与浏览器相同。 解析器对比:   官方文档 # 安装 Beautiful Soup

python爬虫笔记

a 夏天 提交于 2020-01-30 00:54:14
爬虫 http://httpbin.org/ 验证请求 1.urllib库(python3) python内置的HTTP请求库 urllib.request 请求模块 ( https://yiyibooks.cn/xx/python_352/library/urllib.request.html#module-urllib.request ) urllib.error 异常处理模块( https://yiyibooks.cn/xx/python_352/library/urllib.error.html#module-urllib.error ) urllib.parse url解析模块( https://yiyibooks.cn/xx/python_352/library/urllib.parse.html#module-urllib.parse ) urllib.robotparser robots.txt解析模块( https://yiyibooks.cn/xx/python_352/library/urllib.robotparser.html#module-urllib.robotparser ) 请求: import urllib.request urllib.request.urlopen(url, data=None, [timeout, ]*, cafile

Extract Coordinates from KML BatchGeo File with Python

时光怂恿深爱的人放手 提交于 2020-01-28 01:58:06
问题 I've uploaded some addresses to BatchGeo and downloaded the resulting KML file from which I want to extract the coordinates. I managed to prettify the jumbled text file online here, but I don't know how to parse it to extract the co-ordinates. <?xml version="1.0" ?> <kml xmlns="http://earth.google.com/kml/2.0"> <Document> <Placemark> <name>...</name> <description>....</description> <Point> <coordinates>-3.1034345755337,57.144817425039,0</coordinates> </Point><address>...</address> <styleUrl>