python - XPath 仅适用于某些站点
问题描述
我从 lxml 的 XPath 开始。
我只是写了一些指令,但我注意到 XPath 仅适用于某些站点。
我试图解释:
如果我测试这段代码:
import requests
from lxml import html
URL = 'https://it.wikipedia.org/wiki/Pagina_principale'
page = requests.get(URL)
tree = html.fromstring(page.content)
items = tree.xpath('//div[@id="mw-navigation"]//text()')
print(items)
结果是 id="mw-navigation" 的 div 中的所有文本。
有谁知道为什么,如果我有这个 HTML 的其他网站(检查员检查):
<a id="text-search-submit">
<span>Search</span>
</a>
并使用此代码:
import requests
from lxml import html
URL = 'https://www.sneakersnstuff.com/en/472/upcoming-releases'
page = requests.get(URL)
tree = html.fromstring(page.content)
items = tree.xpath('//a[@id="text-search-submit"]//text()')
print(items)
项目是空的?
解决方案
推荐阅读
- json - Unity中使用JsonUtility FromJson反序列化JSON
- java - 尝试使用 POI 复制工作表时将样式从一个工作簿分配给另一个工作簿的问题
- reactjs - 如何在 ReactJs 中打开汉堡菜单?
- javascript - 我正在使用 React,但我的组件未呈现
- json - 仅当原始文件中已存在密钥时,如何使用来自另一个文件的密钥更新 json 文件?
- python - 如何将使用虚数的数学脚本转换为 Python?
- azure-data-factory - 将 json blob 复制到 ADX 表
- java - 如何声明具有相同数据类型的多个参数?(爪哇)
- react-native - 禁用
触摸事件和交互 react-native-maps - android - 片段 exitTransition 与 enterTransition 重叠