【发布时间】:2022-01-18 02:18:01
【问题描述】:
这是我从中提取的 html 代码
from scrapy import Selector
import requests
import pandas as pd
html = '''
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
'''
那我用的是scrapy选择器
sel = Selector( text = html )
然后我使用 xpath 选择一个元素,但它也会返回目标元素之后的所有内容
in:
sel.xpath('/html/body/h1').get()
out:
'<h1>My First Heading</h1>\n\n<p>My first paragraph.</p>\n\n</body>\n</html>\n'
我期待它返回:
'<h1>My First Heading</h1>'
【问题讨论】:
标签: python web-scraping xpath scrapy selector