我正在使用lxml来解析来自另一个系统的导出XML文件:
xmldoc = open(filename)
etree.parse(xmldoc)
Run Code Online (Sandbox Code Playgroud)
但我得到:
lxml.etree.XMLSyntaxError:未定义实体'eacute',第4495行,第46列
显然它有unicode实体名称的问题 - 但我怎么能绕过这个呢?通过open()或parse()?
编辑:我忘了把我的DTD包含在同一个文件夹中 - 它现在就在那里,并有以下声明:
<!ENTITY eacute "é">
Run Code Online (Sandbox Code Playgroud)
并且在xmldoc中被引用(并且始终是),如下所示:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE DScribeDatabase SYSTEM "foo.dtd">
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到同样的问题...... DTD是否也需要在Python中声明?
我有大约4,000个html文档,我试图使用xslt转换为django模板.我遇到的问题是,当我尝试在属性标记中包含模板变量时,xslt正在转义模板变量的'{'花括号; 我的xslt文件如下所示:
<xsl:template match="p">
<p>
<xsl:attribute name="nid"><xsl:value-of select="$node_id"/></xsl:attribute>
<xsl:apply-templates select="*|node()"/>
</p>
<span>
{% get_comment_count for thing '<xsl:value-of select="$node_id"/>' as node_count %}
<a href="">{{ node_count }}</a> //This works as expected
</span>
<div>
<xsl:attribute name="class">HControl</xsl:attribute>
<xsl:text disable-output-escaping="yes">{% if node_count > 0 %}</xsl:text> // have to escape this because of the '>'
<div class="comment-list">
{% get_comment_list for thing '<xsl:value-of select="$node_id"/>' as node_comments %}
{% for comment in node_comments %}
<div class="comment {{ comment.object_id }}"> // this gets escaped
<a>
<xsl:attribute name="name">c{{ comment.id }}</xsl:attribute> …Run Code Online (Sandbox Code Playgroud) 如何<br/>在Python中添加一个像ElementTree 一样的空元素?
谢谢!
我正在使用lxml来解析html并对其进行编辑以生成新文档.本质上,我试图使用它有点像javascript DOM - 我知道这不是真正的预期用途,但到目前为止它的大部分工作都很好.
目前,我使用iterdescendants()获取可迭代的元素列表,然后依次处理每个元素.
但是,如果在迭代期间删除了一个元素,则仍会考虑其子元素,因为删除不会像您期望的那样影响迭代.为了得到我想要的结果,这个hack有效:
from lxml.html import fromstring, tostring
import urllib2
import re
html = '''
<html>
<head>
</head>
<body>
<div>
<p class="unwanted">This content should go</p>
<p class="fine">This content should stay</p>
</div>
<div id = "second" class="unwanted">
<p class = "alreadydead">This content should not be looked at</p>
<p class = "alreadydead">Nor should this</>
<div class="alreadydead">
<p class="alreadydead">Still dead</p>
</div>
</div>
<div>
<p class="yeswanted">This content should also stay</p>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
for element in allElements:
s = "%s%s" % (element.get('class', …Run Code Online (Sandbox Code Playgroud) 我是新限于lxml并要提取<p>PARAGRAPHS</p>并<li>PARAGRAPHS</li>从给定的URL,并将其用于进一步的措施.
我从帖子中跟踪了一个例子,并尝试了下面的代码而没有运气:
html = lxml.html('http://www.google.com/intl/en/about/corporate/index.html')
url = 'http://www.google.com/intl/en/about/corporate/index.html'
print html.parse.xpath('//p/text()')
Run Code Online (Sandbox Code Playgroud)
我试着查看lxml.html中的示例,但没有找到任何使用url的示例.
你能给我一些关于我应该使用什么方法的提示吗?谢谢.
我正在遵循lxml验证文档来构建一个类,该类根据Math ML 3.0模式验证给定的XML字符串.这是班级:
class XMLSchema(object):
def __init__(self, path_to_xsd_file):
with open(path_to_xsd_file) as f:
xmlschema_doc = etree.parse(f)
self.xmlschema = etree.XMLSchema(xmlschema_doc)
def validate(self, well_formed_xml_string):
"""Validates a well-formed XML string against an XML schema.
Returns True if xml_string is valid, False if not.
"""
xml = etree.parse(StringIO(well_formed_xml_string))
return self.xmlschema.validate(xml)
Run Code Online (Sandbox Code Playgroud)
实例化它会产生以下结果:
>>> x = XMLSchema('mathml3.xsd')
Traceback (most recent call last):
...
lxml.etree.XMLSchemaParseError: complex type
'annotation-xml.model': The content model is not determinist., line 42
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
我正在尝试使用xml从雅虎财务中划出"部门"和"行业"字段.
我注意到href url始终是http://biz.yahoo.com/ic/ xyz .html,其中xyz是数字.
您能否建议包含1位或更多位数的通配符?我已经尝试了几种基于Google和堆栈搜索的方法,但没有任何效果.
import lxml.html
url = 'http://finance.yahoo.com/q?s=AAPL'
root = lxml.html.parse(url).getroot()
for a in root.xpath('//a[@href="http://biz.yahoo.com/ic/' + 3 digit integer wildcard " +'.html"]')
print a.text
Run Code Online (Sandbox Code Playgroud) 我有一个带有javascript的XSLT,它使用"<" 和">" 在里面循环
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head> </head>
<body>
<script language="javascript" type="text/javascript">
function example() {
var trs = document.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
}
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我正在使用PYTHON LXML库使用XSLT和XML生成HTML.
import lxml.etree as ET
xml = ET.parse('sample.xml')
xslt = ET.parse('sample.xsl')
transform = ET.XSLT(xslt)
content = transform(xml)
f = open('output.html','w')
f.write(ET.tostring(content , pretty_print=True))
f.close()
Run Code Online (Sandbox Code Playgroud)
但是LXML无法替换输出HTML文件中的特殊字符
< 到'<'和> 到'>'
是否有任何使用LXML替换"<"的标准做法 到'<'?
为了解决这个问题,我必须在写入文件之前编写另一段代码.
content = content.replace(">", ">")
content = content.replace("<", "<")
Run Code Online (Sandbox Code Playgroud) XML
<root>
<p>nodea text 1</p>
<p>nodea text 2</p>
<nodea>
</nodea>
<p>nodeb text 1</p>
<p>nodeb text 2</p>
<nodeb>
</nodeb>
</root>
Run Code Online (Sandbox Code Playgroud)
我想获得nodea或nodeb的前一个兄弟p标签,如果有的话.例如,对于上述xml,各个节点的前一个兄弟节点是
nodea在兄弟姐妹之前
<p>nodea text 1</p>
<p>nodea text 2</p>
Run Code Online (Sandbox Code Playgroud)
nodeb在兄弟姐妹之前
<p>nodeb text 1</p>
<p>nodeb text 2</p>
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的xpath,但它给了我前面的nodea标签而不是nodeb.
nodeb = xml.find('nodeb')
nodeb.xpath('preceding-sibling::p[not(preceding-sibling::nodea)][1]')
Run Code Online (Sandbox Code Playgroud)
如果节点之前没有前面的p标记,那么它应该返回空列表.例如,对于下面的xml,nodeb没有前面的兄弟p标签.
<root>
<p>nodea text 1</p>
<nodea>
</nodea>
<nodeb>
</nodeb>
</root>
Run Code Online (Sandbox Code Playgroud)
如果有人也可以解释为什么我的xpath不起作用以及在编写xpath时我应该记住什么,这将是很好的?
我正在尝试解析Google搜索结果的首页。具体来说,就是标题和提供的小摘要。这是我到目前为止的内容:
from urllib.request import urlretrieve
import urllib.parse
from urllib.parse import urlencode, urlparse, parse_qs
import webbrowser
from bs4 import BeautifulSoup
import requests
address = 'https://google.com/#q='
# Default Google search address start
file = open( "OCR.txt", "rt" )
# Open text document that contains the question
word = file.read()
file.close()
myList = [item for item in word.split('\n')]
newString = ' '.join(myList)
# The question is on multiple lines so this joins them together with proper spacing
print(newString)
qstr = urllib.parse.quote_plus(newString)
# Encode …Run Code Online (Sandbox Code Playgroud) lxml ×10
python ×9
xml ×4
xpath ×2
escaping ×1
html-parsing ×1
javascript ×1
mathml ×1
parsing ×1
python-3.x ×1
unicode ×1
wildcard ×1
xml-parsing ×1
xsd ×1
xslt ×1