Python lxml Xpath 导入问题 - 我需要删除附加到导入值的前导和尾随 [' ']
Python lxml Xpath import problem - I need to remove leading and trailing [' '] appended to the imoprted value
我正在尝试通过 Python 和 lxml 监控特定规范 URL 是否在网页上发生了更改。
我们的想法是在 Excel sheet 中有一个包含正确规范 URL 的 URL 列表,并将其导入 Python然后定期检查实时站点(通过 lxml 和 Xpath)。如果检测到变化,则标志设置为 FALSE。
问题:当我通过 lxml 导入时,会导入额外的字符,这意味着我的 True / False 匹配永远不起作用。例如 lxml 将创建 ['https://www.example.com/canonical.html']
而不是 https://www.example.com/canonical.html
new_canonical = tree.xpath('//link[@rel="canonical"]/@href')
我是 python 的菜鸟,但一整天都在竭尽全力,甚至将额外的 [' '] 字符附加到 URL 中的 excel sheet(这样他们就匹配了——但这是行不通的)。
简而言之,我需要new_canonical = tree.xpath('//link[@rel="canonical"]/@href')
来生产https://www.example.com/canonical.html instead of ['https://www.example.com/canonical.html']
xpath
方法returns一个PythonList.
您需要做的就是从列表中获取第一个元素。
new_canonical = tree.xpath('//link[@rel="canonical"]/@href')
my_url = new_canonical[0]
我正在尝试通过 Python 和 lxml 监控特定规范 URL 是否在网页上发生了更改。
我们的想法是在 Excel sheet 中有一个包含正确规范 URL 的 URL 列表,并将其导入 Python然后定期检查实时站点(通过 lxml 和 Xpath)。如果检测到变化,则标志设置为 FALSE。
问题:当我通过 lxml 导入时,会导入额外的字符,这意味着我的 True / False 匹配永远不起作用。例如 lxml 将创建 ['https://www.example.com/canonical.html']
而不是 https://www.example.com/canonical.html
new_canonical = tree.xpath('//link[@rel="canonical"]/@href')
我是 python 的菜鸟,但一整天都在竭尽全力,甚至将额外的 [' '] 字符附加到 URL 中的 excel sheet(这样他们就匹配了——但这是行不通的)。
简而言之,我需要new_canonical = tree.xpath('//link[@rel="canonical"]/@href')
来生产https://www.example.com/canonical.html instead of ['https://www.example.com/canonical.html']
xpath
方法returns一个PythonList.
您需要做的就是从列表中获取第一个元素。
new_canonical = tree.xpath('//link[@rel="canonical"]/@href')
my_url = new_canonical[0]