标签: lxml

如何使用lxml,python解析html

我有一些html文件:

<html>
 <body>
   <span class="text">One</span>some text1</br>
   <span class="cyrillic">???</span>some text2</br>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何使用带Python的lxml获取"some text1"和"some text2"?

python parsing lxml

1
推荐指数
1
解决办法
4378
查看次数

构建LXML.预编译的.c Cython文件丢失

我必须从源代码构建lxml.https://github.com/lxml/lxml 正如文件/doc/build.txt所说,源代码应该提供预编译的.c文件.为什么我找不到它们?在/src/lxml那里只有.pyx文件.哪里.c文件?? ??

实际上我lxml.etree.cpython build命令期间有丢失的文件.

谢谢

python lxml cython

1
推荐指数
1
解决办法
1642
查看次数

使用 Python 创建 XML 文档时添加命名空间

我需要使用 Python 创建一个 XML 文档,但我无法弄清楚如何添加一个

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

以及如何将命名空间元素添加到文档标签

<Document xmlns="urn:iso:std:iso:2013:008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <page1 xmlns="urn:iso:std:iso:2013:008.001.02" </page1>
</Document>
Run Code Online (Sandbox Code Playgroud)

任何例子请

python xml lxml

1
推荐指数
1
解决办法
1704
查看次数

命名空间错误lxml xpath python

我正在将word文档转换为xml,以使用以下代码进行比较:

word = win32com.client.Dispatch('Word.Application')
wd = word.Documents.Open(inFile)
# Converts the word infile to xml outfile
wd.SaveAs(outFile,11)
wd.Close()
dom=parse(outFile)
Run Code Online (Sandbox Code Playgroud)

我得到的xml文件看起来像:

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument w:embeddedObjPresent="no" w:macrosPresent="no" w:ocxPresent="no" xml:space="preserve" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint">
    <w:ignoreSubtree w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
    <w:shapeDefaults>
        <o:shapedefaults spidmax="1027" v:ext="edit"/>
        <o:shapelayout v:ext="edit">
            <o:idmap data="1" v:ext="edit"/>
        </o:shapelayout>
    </w:shapeDefaults>
    <w:body>
        <wx:sect>
            <w:tbl>

            <w:tblGrid>
                <w:gridCol w:w="200"/>
                                       ...
            </w:tblGrid>

                <w:pict>
                        <v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
                            <v:stroke joinstyle="miter"/>
                            <v:formulas>
                                <v:f eqn="if lineDrawn pixelLineWidth 0"/>
                                ... …
Run Code Online (Sandbox Code Playgroud)

python xml xpath lxml

1
推荐指数
1
解决办法
3983
查看次数

python lxml以预定义的顺序写入文件

我想编写以下lxml etree 子元素

<ElementProtocolat0x3803048>,
<ElementStudyEventDefat0x3803108>,
<ElementFormDefat0x3803248>,
<ElementItemGroupDefat0x38032c8>,
<ElementClinicalDataat0x3803408>,
<ElementItemGroupDataat0x38035c8>,
<ElementFormDefat0x38036c8>,
Run Code Online (Sandbox Code Playgroud)

预定义的顺序到我的 odm xml 文件。IE

<ElementProtocolat0x3803048>,
<ElementStudyEventDefat0x3803108>,
<ElementFormDefat0x3803248>,
<ElementFormDefat0x38036c8>,
<ElementItemGroupDefat0x38032c8>,
<ElementItemGroupDataat0x38035c8>,
<ElementClinicalDataat0x3803408>,
....
Run Code Online (Sandbox Code Playgroud)

有没有办法对元素进行排序,即使用预定义的列表?

predefined_order = ['Protocol', 'StudyEventDef','FormDef','ItemGroupDef','ItemDef','CodeList']
Run Code Online (Sandbox Code Playgroud)

python sorting lxml elementtree

1
推荐指数
1
解决办法
1890
查看次数

Findall 等效于 xpath ,Lxml

我正在提取关于标签的文本,我需要以列表形式获取它们 wrt p 标签。我有这个 xpath 表达式:

 find =  etree.XPath("//w:p//.//*[local-name() = 'ins']//text()" ,namespaces={'w':"http://schemas.openxmlformats.org/wordprocessingml/2006/main"}) 
Run Code Online (Sandbox Code Playgroud)

我想在findall表达式中使用它。我试过:

inserted_list_1=[]
for p in lxml_tree.findall('.//{' + w + '}p'):
    inserted_list_1.append([t.text for t in p.findall('.//{' + w + '}ins')])
Run Code Online (Sandbox Code Playgroud)

但所有这些返回的是一个充满None值的列表,而前 xpath 工作得很好。
我认为缺少一些中间路径。

python xml xpath lxml findall

1
推荐指数
1
解决办法
2431
查看次数

使用Python请求提取href URL

我想使用python中的request包从xpath提取URL。我可以得到文本,但没有尝试给出URL。有人可以帮忙吗?

ipdb> webpage.xpath(xpath_url + '/text()')
['Text of the URL']
ipdb> webpage.xpath(xpath_url + '/a()')
*** lxml.etree.XPathEvalError: Invalid expression
ipdb> webpage.xpath(xpath_url + '/href()')
*** lxml.etree.XPathEvalError: Invalid expression
ipdb> webpage.xpath(xpath_url + '/url()')
*** lxml.etree.XPathEvalError: Invalid expression
Run Code Online (Sandbox Code Playgroud)

我使用本教程开始学习:http : //docs.python-guide.org/en/latest/scenarios/scrape/

看起来应该很容易,但是在搜索过程中什么都没有发生。

谢谢。

python xpath lxml python-3.x python-requests

1
推荐指数
2
解决办法
1万
查看次数

python beautifulsoup:lxml html.parser

我必须使用beautifulsoup,但是我不知道我必须使用哪个解析器。我对lxml和html.parser犹豫不决,或者为什么不两者都选择。如何知道网页是否符合lxml?如何知道网页是否符合html解析器?非常感谢

python lxml beautifulsoup html-parser

1
推荐指数
1
解决办法
1054
查看次数

Python LXML通过ID标记查找元素

我正在使用python程序来保存存储空间的清单。在XML文档中,将保留碳粉量,我希望我的python程序能够添加,删除和显示不同打印机和不同颜色的碳粉量。

我的XML如下所示:

<?xml version="1.0"?>
<printer>
    <t id="095205615111"> <!-- 7545 Magenta -->
        <toner>7545 Magenta Toner</toner>
        <amount>3</amount>
    </t>
    <t id="095205615104"> <!-- 7545 Yellow -->
        <toner>7545 Yellow Toner</toner>
        <amount>7</amount>
    </t>
</printer>
Run Code Online (Sandbox Code Playgroud)

id是我们用于库存的条形码中的编号。

到目前为止,我已经希望我的程序使用这些步骤:

  1. 检查id是否存在(id-value是我的python程序中从txt文件中的内容通过管道传递的变量)

  2. 将xml文档中的amount值更改为+1或-1

无论我尝试什么,它都无法充分发挥作用。您对我可以使用的东西有什么建议吗?

python xml lxml xml-parsing

1
推荐指数
1
解决办法
7275
查看次数

使用BeautifulSoup获取跨度之间的文本

我正在尝试使用Python中的BeautifulSoup抓取各种站点。说我有以下html摘录:

<div class="member_biography">
<h3>Biography</h3>
<span class="sub_heading">District:</span> AnyState - At Large<br/>
<span class="sub_heading">Political Highlights:</span> AnyTown City Council, 19XX-XX<br/>
<span class="sub_heading">Born:</span> June X, 19XX; AnyTown, Calif.<br/>
<span class="sub_heading">Residence:</span> Some Town<br/>
<span class="sub_heading">Religion:</span> Episcopalian<br/>
<span class="sub_heading">Family:</span> Wife, Some Name; two children<br/>
<span class="sub_heading">Education:</span> Some State College, A.A. 19XX; Some Other State College, B.A. 19XX<br/>
<span class="sub_heading">Elected:</span> 19XX<br/>
</div>
Run Code Online (Sandbox Code Playgroud)

我需要结果采用以下格式:

District:              AnyState - At Large
Political Highlights:  AnyTown City Council, 19XX-XX
Born:                  June X, 19XX; AnyTown, Calif.
Residence:             Some Town
Religion:              Episcopalian …
Run Code Online (Sandbox Code Playgroud)

python lxml beautifulsoup

1
推荐指数
1
解决办法
174
查看次数