如何使用选择器查找特定文本?
How to find certain text using selectors?
我在 python 中编写了一个脚本来查找 td
标签中的文本,该标签是第一个 td
标签的 next sibling
使用 BeautifulSoup结合 css 选择器。如果我 运行 脚本,我发现它可以工作。但是,当我使用 lxml
库执行相同操作时,它不再有效。我怎样才能让我的后一个脚本工作?谢谢。
这是内容:
html_content="""
<tr>
<td width="25%" valign="top" bgcolor="lightgrey" nowrap="">
<font face="Arial" size="-1" color="224119">
<b>Owner Address </b>
</font>
</td>
<td width="75%" valign="top" nowrap="">
<font face="Arial" size="-1" color="black">
1698 EIDER DOWN DR<br>SUMMERVILLE SC 29483
</font>
</td>
</tr>
"""
正在使用 bs4:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content,"lxml")
item = soup.select("td")[0].find_next_sibling().text
print(item)
结果:
1698 EIDER DOWN DRSUMMERVILLE SC 29483
下面的脚本可以找到地址字符串:
from lxml.html import fromstring
root = fromstring(html_content)
item = root.cssselect("td b:contains('Address')")[0].text
print(item)
结果:
Owner Address
查找下一个兄弟时不起作用(应用“+”号查找下一个兄弟:
from lxml.html import fromstring
root = fromstring(html_content)
item = root.cssselect("td b:contains('Owner Address')+td")[0].text
print(item)
结果:
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python35-32\new_line_one.py", line 28, in <module>
item = root.cssselect("td b:contains('Owner Address')+td")[0].text
IndexError: list index out of range
我怎样才能找到下一个兄弟姐妹?顺便说一句,我只是在 css 选择器之后而不是 xpath。谢谢。
来自 css3 选择器 docs:
8.3.1. Adjacent sibling combinator
The adjacent sibling combinator is made of the "plus sign" (U+002B, +)
character that separates two sequences of simple selectors. The
elements represented by the two sequences share the same parent in the
document tree and the element represented by the first sequence
immediately precedes the element represented by the second one.
这意味着在您的选择器 td b:contains('Owner Address')+td
中,您要求的 td
与包含 'Address' 的 b
具有相同的父级并且是 'Address' 的子级另一个 td
。该节点不存在。要使其工作,您需要确保您的第一个部分选择器匹配 td
,而不是 b
节点。由于它们相互包含,因此以下将起作用:
td:contains('Owner Address') + td
请注意,此 td
没有文本(只有子节点),因此上面的代码片段仅打印空格。
我在 python 中编写了一个脚本来查找 td
标签中的文本,该标签是第一个 td
标签的 next sibling
使用 BeautifulSoup结合 css 选择器。如果我 运行 脚本,我发现它可以工作。但是,当我使用 lxml
库执行相同操作时,它不再有效。我怎样才能让我的后一个脚本工作?谢谢。
这是内容:
html_content="""
<tr>
<td width="25%" valign="top" bgcolor="lightgrey" nowrap="">
<font face="Arial" size="-1" color="224119">
<b>Owner Address </b>
</font>
</td>
<td width="75%" valign="top" nowrap="">
<font face="Arial" size="-1" color="black">
1698 EIDER DOWN DR<br>SUMMERVILLE SC 29483
</font>
</td>
</tr>
"""
正在使用 bs4:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content,"lxml")
item = soup.select("td")[0].find_next_sibling().text
print(item)
结果:
1698 EIDER DOWN DRSUMMERVILLE SC 29483
下面的脚本可以找到地址字符串:
from lxml.html import fromstring
root = fromstring(html_content)
item = root.cssselect("td b:contains('Address')")[0].text
print(item)
结果:
Owner Address
查找下一个兄弟时不起作用(应用“+”号查找下一个兄弟:
from lxml.html import fromstring
root = fromstring(html_content)
item = root.cssselect("td b:contains('Owner Address')+td")[0].text
print(item)
结果:
Traceback (most recent call last):
File "C:\Users\ar\AppData\Local\Programs\Python\Python35-32\new_line_one.py", line 28, in <module>
item = root.cssselect("td b:contains('Owner Address')+td")[0].text
IndexError: list index out of range
我怎样才能找到下一个兄弟姐妹?顺便说一句,我只是在 css 选择器之后而不是 xpath。谢谢。
来自 css3 选择器 docs:
8.3.1. Adjacent sibling combinator
The adjacent sibling combinator is made of the "plus sign" (U+002B, +) character that separates two sequences of simple selectors. The elements represented by the two sequences share the same parent in the document tree and the element represented by the first sequence immediately precedes the element represented by the second one.
这意味着在您的选择器 td b:contains('Owner Address')+td
中,您要求的 td
与包含 'Address' 的 b
具有相同的父级并且是 'Address' 的子级另一个 td
。该节点不存在。要使其工作,您需要确保您的第一个部分选择器匹配 td
,而不是 b
节点。由于它们相互包含,因此以下将起作用:
td:contains('Owner Address') + td
请注意,此 td
没有文本(只有子节点),因此上面的代码片段仅打印空格。