python提取html中一段字符_lxml.html通过搜索关键字提取字符串

我有一部分html如下

The Keyword: The text

我想得到字符串“关键字:文本”。在

我知道我可以使用Chrome inspect或FF firebug获取上述html的xpath,然后选择(xpath.extract(),然后剥离html标记来获得字符串。但是,这种方法不够通用,因为xpath在不同的页面上并不一致。在

因此,我考虑以下方法:

首先,搜索“The Keyword:”using(代码是为crapy HtmlXPathSelector编写的,因为我不确定如何在lxml.html)在

^{pr2}$

我什么时候能得到回报:>>> pprint( hxs.select('//*[contains(text(), "The Keyword:")]') )

The Keyword:'>

我的问题是如何得到想要的字符串:“关键字:文本”。我在考虑如何确定xpath,如果xpath是已知的,那么我当然可以得到想要的字符串。在

我愿意接受除此之外的任何解决方案lxml.html. 在

谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释下F:\python_projects\venv\Scripts\python.exe F:\result\eye_first_move_to_objects_time.py Traceback (most recent call last): File "F:\result\eye_first_move_to_objects_time.py", line 73, in <module> coordinate_x = float(fix_record[row_index][5].value) ValueError: could not convert string to float: '.' Error in atexit._run_exitfuncs: Traceback (most recent call last): File "F:\python_projects\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 32, in _openpyxl_shutdown os.remove(path) PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\\Users\\dell\\AppData\\Local\\Temp\\openpyxl.byyckh9l' Exception ignored in: <generator object WorksheetWriter.get_stream at 0x000001FBA5104820> Traceback (most recent call last): File "F:\python_projects\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 300, in get_stream File "src\lxml\serializer.pxi", line 1834, in lxml.etree._FileWriterElement.__exit__ File "src\lxml\serializer.pxi", line 1570, in lxml.etree._IncrementalFileWriter._write_end_element lxml.etree.LxmlSyntaxError: inconsistent exit action in context manager Exception ignored in: <generator object WriteOnlyWorksheet._write_rows at 0x000001FBA5104270> Traceback (most recent call last): File "F:\python_projects\venv\lib\site-packages\openpyxl\worksheet\_write_only.py", line 75, in _write_rows File "src\lxml\serializer.pxi", line 1834, in lxml.etree._FileWriterElement.__exit__ File "src\lxml\serializer.pxi", line 1568, in lxml.etree._IncrementalFileWriter._write_end_element lxml.etree.LxmlSyntaxError: not in an element Process finished with exit code 1
最新发布
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值