lxml

lxml: Undefined variable etree

吃可爱长大的小学妹 提交于 2020-01-12 10:29:06
问题 I'm returning to Python after a little hiatus. Some projects that used to work now have a problem with lxml. I have the latest source from github installed locally and have it in an Eclipse project. This project has the following in PyDev-PYTHONPATH: /${PROJECT_DIR_NAME} /${PROJECT_DIR_NAME}/src In a project that uses lxml , in the Project References, I have the lxml project checked. A file in this project has: import lxml which is underlined in yellow with the warning: Unused import: lxml

Is there a way to recover iterparse on invalid Char values?

空扰寡人 提交于 2020-01-11 12:31:51
问题 I'm using lxml's iterparse to parse some big XML files (3-5Gig). Since some of these files have invalid characters a lxml.etree.XMLSyntaxError is thrown. When using lxml.etree.parse I can provide a parser which recovers on invalid characters: parser = lxml.etree.XMLParser(recover=True) root = lxml.etree.parse(open("myMalformed.xml, parser) Is there a way to get the same functionality for iterparse? Edit: Encoding is not an Issue here. There are invalid characters in these XML files which can

How to update XML file with lxml

痴心易碎 提交于 2020-01-11 04:53:06
问题 I want to update xml file with new information by using lxml library. For example, I have this code: >>> from lxml import etree >>> >>> tree = etree.parse('books.xml') where 'books.xml' file, has this content: http://www.w3schools.com/dom/books.xml I want to update this file with new book: >>> new_entry = etree.fromstring('''<book category="web" cover="paperback"> ... <title lang="en">Learning XML 2</title> ... <author>Erik Ray</author> ... <year>2006</year> ... <price>49.95</price> ... <

使用openpyxl时遇到的坑

一世执手 提交于 2020-01-08 10:56:30
最近在用 python 处理 Excel 表格是遇到了一些问题 1, xlwt 最多只能写入65536行数据, 所以在处理大批量数据的时候没法使用 2, openpyxl 这个库, 在使用的时候一直报错, 看下面代码 from openpyxl import Workbook import datetime wb = Workbook() ws = wb.active ws['A1'] = 42 ws.append([1,2,3]) ws['A2'] = datetime.datetime.now() wb.save('test.xlsx') 报错信息如下 File "src\lxml\serializer.pxi", line 1652, in lxml.etree._IncrementalFileWriter.write TypeError: got invalid input value of type <class 'xml.etree.ElementTree.Element'>, expected string or Element 有没有人知道是什么原因呀? 惆怅!!! got invalid input value of type <class ‘xml.etree.ElementTree.Element’>, expected string or Element

用python抓取“3d”彩票数据,怎么分析你说了算!

旧街凉风 提交于 2020-01-07 11:54:42
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 快下班了,正好准备去买彩票,就顺手写了2个脚本,一个用来下载最近的彩票数据,一个用来统计彩票数字,分享给大家! 彩票数据获取并写入excel表格 数据来源自己看吧~用外链通不过。。。 所用库:xlwt,requests,lxml 有几点需要注意的: 1、 构建列表 。因为存入excel文件的时候用的是列表,所以新建一个函数,分别取网页5个数据:时间、期数、开奖数123,然后每一页嵌套写入列表类似结构为[[时间、期数、开奖数1,2,3],[时间、期数、开奖数1,2,3]……],在循环页数,获取所有的数据!注意构建列表的形式和列表结果,这个在你写入表格的时候很重要! 2、 写入数据 。xlwt写入文件的方法为ws.write(行,列,数据),按行写入文件,所以新建一个变量line(代码第36行),每写入一行自增1。 其他方面都很简单,没有反爬,就是为了获取数据,好做分析! 最后excel表中的数据,是这样的: 最后大概有4840行数据,足够我们分析的了! 数据处理 用xlrd库就可以~话说xlwt库和xlrd库好像就是一个写数据,一个读数据。。。 就写了一个抓热门数字的,也就是取频率最高的。如果您有更好的想法或者玩法,可以自行去实现哦! 先读取数据,然后取到每一行的2.3.4列,每一列写入一个列表(现在有些后悔

BeautifulSoup4

房东的猫 提交于 2020-01-07 07:48:30
目录 1. BeautifulSoup4简介 2. 安装 3. 使用 3.1 基本使用 3.2 遍历文档树 3.3 搜索文档树 3.3.1 五种过滤器 3.3.2 find_all(name, attrs, recursive, text, **kwargs) 3.3.3 find(name, attrs, recursive, text, **kwargs) 3.3.4 CSS选择器 1. BeautifulSoup4简介 官方文档: https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库。它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式。Beautiful Soup会帮你节省数小时甚至数天的工作时间.你可能在寻找 Beautiful Soup3 的文档,Beautiful Soup 3 目前已经停止开发,官网推荐在现在的项目中使用Beautiful Soup 4, 移植到BS4。 2. 安装 安装Beautiful Soup 4 pip install beautifulsoup4 安装解析器 Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器

用python统计3d彩票热门数据,看今天运势如何

≯℡__Kan透↙ 提交于 2020-01-07 06:47:57
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 快下班了,正好准备去买彩票,就顺手写了2个脚本,一个用来下载最近的彩票数据,一个用来统计彩票数字,分享给大家! 彩票数据获取并写入excel表格 数据来源自己看吧~用外链通不过。。。 所用库:xlwt,requests,lxml 有几点需要注意的: 1、构建列表。因为存入excel文件的时候用的是列表,所以新建一个函数,分别取网页5个数据:时间、期数、开奖数123,然后每一页嵌套写入列表类似结构为[[时间、期数、开奖数1,2,3],[时间、期数、开奖数1,2,3]……],在循环页数,获取所有的数据!注意构建列表的形式和列表结果,这个在你写入表格的时候很重要! 2、写入数据。xlwt写入文件的方法为ws.write(行,列,数据),按行写入文件,所以新建一个变量line(代码第36行),每写入一行自增1。 其他方面都很简单,没有反爬,就是为了获取数据,好做分析! 最后excel表中的数据 是这样的: 最后大概有4840行数据,足够我们分析的了! 数据处理 用xlrd库就可以~话说xlwt库和xlrd库好像就是一个写数据,一个读数据。。。 就写了一个抓热门数字的,也就是取频率最高的。如果您有更好的想法或者玩法,可以自行去实现哦! 先读取数据,然后取到每一行的2.3.4列,每一列写入一个列表(现在有些后悔

How to get more info from lxml errors?

邮差的信 提交于 2020-01-07 06:23:28
问题 Because I'm not able to use an XSL IDE, I've written a super-simple Python script using lxml to transform a given XML file with a given XSL transform, and write the results to a file. As follows (abridged): p = XMLParser(huge_tree=True) xml = etree.parse(xml_filename, parser=p) xml_root = xml.getroot() print(xml_root.tag) xslt_root = etree.parse(xsl_filename) transform = etree.XSLT(xslt_root) newtext = transform(xml) with open(output, 'w') as f: f.write(str(newtext)) I'm getting the following

How to get more info from lxml errors?

有些话、适合烂在心里 提交于 2020-01-07 06:23:15
问题 Because I'm not able to use an XSL IDE, I've written a super-simple Python script using lxml to transform a given XML file with a given XSL transform, and write the results to a file. As follows (abridged): p = XMLParser(huge_tree=True) xml = etree.parse(xml_filename, parser=p) xml_root = xml.getroot() print(xml_root.tag) xslt_root = etree.parse(xsl_filename) transform = etree.XSLT(xslt_root) newtext = transform(xml) with open(output, 'w') as f: f.write(str(newtext)) I'm getting the following

Make Urllib2 move through pages

只谈情不闲聊 提交于 2020-01-07 03:42:31
问题 I am trying to scrape http://targetstudy.com/school/schools-in-chhattisgarh.html I am usling lxml.html, urllib2 I want somehow, follow all the pages by clicking the next page link and download its source. And make it stop at the last page. The href for next page is ['?recNo=25'] Could someone please advise how to do that, Thanks in advance. Here is my code, import urllib2 import lxml.html import itertools url = "http://targetstudy.com/school/schools-in-chhattisgarh.html" req = urllib2.Request