Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python XPATH-html有很多子项_Python_Html_Python 2.7_Xpath_Web Scraping - Fatal编程技术网

Python XPATH-html有很多子项

Python XPATH-html有很多子项,python,html,python-2.7,xpath,web-scraping,Python,Html,Python 2.7,Xpath,Web Scraping,考虑页面变量中的html 如何访问tds 我想像xpath(“/table/tr/td/text())”那样访问它们 我不想指出其他trs 不幸的是,这个表达式xpath('.//table/tr/tr/tr/td/text()')也不起作用 Python代码: import __future__ from lxml import html import requests from bs4 import BeautifulSoup page = """ <!DOCTYPE html>

考虑页面变量中的html

如何访问tds

我想像
xpath(“/table/tr/td/text())”那样访问它们

我不想指出其他trs

不幸的是,这个表达式
xpath('.//table/tr/tr/tr/td/text()')
也不起作用

Python代码:

import __future__
from lxml import html
import requests
from bs4 import BeautifulSoup

page = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>cv</title>
</head>
<body>

    <table>
        <tr>
            <tr>
                <tr>
                    <td>table1 td1</td>
                    <td>table1 td2</td>
                </tr>
            </tr>
        </tr>
    </table>

    <table>
        <tr>
            <tr>
                <tr>
                    <td>table2 td1</td>
                    <td>table2 td2</td>
                </tr>
            </tr>
        </tr>
    </table>

    <table>
        <tr>
            <tr>
                <tr>
                    <td>table3 td1</td>
                    <td>table3 td2</td>
                </tr>
            </tr>
        </tr>
    </table>
</body>
</html>
"""

soup = str(BeautifulSoup(page, 'html.parser'))
tree = html.fromstring(soup)

things = tree.xpath('.//table/tr/tr/tr/td/text()')

print(things)

for thing in things:
        print(thing)

print('That's all')
import\uu未来__
从lxml导入html
导入请求
从bs4导入BeautifulSoup
第“”页
个人简历
表1 td1
表1 td2
表2 td1
表2 td2
表3 td1
表3 td2
"""
soup=str(美化组(页面'html.parser'))
tree=html.fromstring(汤)
things=tree.xpath(“.//table/tr/tr/tr/td/text()”)
印刷品(物品)
对于事物中的事物:
印刷品(东西)
打印(“仅此而已”)
我要从根开始

使用xpath
//td/text()

//td
代表“查找任何深度的
td
元素”

对我有用

打印根据
表分组的
td
元素: 请注意,在本例中,xpath中的
非常重要。

使用xpath
//td/text()

//td
代表“查找任何深度的
td
元素”

对我有用

打印根据
表分组的
td
元素:
请注意,在本例中,xpath中的
非常重要。

您不必将
BeautifulSoup
转换为
str

soup = str(BeautifulSoup(page, 'html.parser'))
您可以使用以下内容:

>>> soup = BeautifulSoup(page, 'html.parser')
>>> for td in soup.find_all('td'):
...     print(td)
... 
<td>table1 td1</td>
<td>table1 td2</td>
<td>table2 td1</td>
<td>table2 td2</td>
<td>table3 td1</td>
<td>table3 td2</td>
>soup=BeautifulSoup(页面'html.parser')
>>>对于汤中的td。查找所有('td'):
...     印刷品(td)
... 
表1 td1
表1 td2
表2 td1
表2 td2
表3 td1
表3 td2

或者,如果希望在元素中包含文本,也可以使用
print(td.text)

您不必将
BeautifulSoup
转换为
str

soup = str(BeautifulSoup(page, 'html.parser'))
您可以使用以下内容:

>>> soup = BeautifulSoup(page, 'html.parser')
>>> for td in soup.find_all('td'):
...     print(td)
... 
<td>table1 td1</td>
<td>table1 td2</td>
<td>table2 td1</td>
<td>table2 td2</td>
<td>table3 td1</td>
<td>table3 td2</td>
>soup=BeautifulSoup(页面'html.parser')
>>>对于汤中的td。查找所有('td'):
...     印刷品(td)
... 
表1 td1
表1 td2
表2 td1
表2 td2
表3 td1
表3 td2

或者,如果希望在元素中包含文本,也可以使用
print(td.text)

tr
中的
tr
是无效的HTML

这似乎是由
html.fromstring()
解析器“修复”的

您可以使用以下xpath测试这一点:

things = tree.xpath('//table/tr/*')
和输出:

for thing in things:
   print(thing.tag)
由此产生:

td
td
td
td
td

tr
内部的
tr
是无效的HTML

这似乎是由
html.fromstring()
解析器“修复”的

您可以使用以下xpath测试这一点:

things = tree.xpath('//table/tr/*')
和输出:

for thing in things:
   print(thing.tag)
由此产生:

td
td
td
td
td

这对我没有帮助,我要它从根开始!!!因为随后我将从每个表索引访问tds,如:
xpath(“/table[1]/tr/td/text()”)
xpath(“/table[1]/td/text()”)
@hr\u 117好的,如果输出应按表分组,那么我们按表执行xpath。请看扩展答案。这对我没有帮助,我希望它从根开始!!!因为随后我将从每个表索引访问tds,如:
xpath(“/table[1]/tr/td/text()”)
xpath(“/table[1]/td/text()”)
@hr\u 117好的,如果输出应按表分组,那么我们按表执行xpath。参见扩展答案。