我想编写一个代码片段,它将<content>在下面所有三个实例(包括代码标记)中的lxml中获取标记内的所有文本.我已经尝试了tostring(getchildren())但是会遗漏标签之间的文字.我没有太多运气在API中搜索相关功能.你能救我吗?
<!--1-->
<content>
<div>Text inside tag</div>
</content>
#should return "<div>Text inside tag</div>
<!--2-->
<content>
Text with no tag
</content>
#should return "Text with no tag"
<!--3-->
<content>
Text outside tag <div>Text inside tag</div>
</content>
#should return "Text outside tag <div>Text inside tag</div>"
Run Code Online (Sandbox Code Playgroud) 如何检索标记中包含的所有HTML?
hxs = HtmlXPathSelector(response)
element = hxs.select('//span[@class="title"]/')
Run Code Online (Sandbox Code Playgroud)
编辑:如果我查看文档,我只看到返回新的xpathselectorlist的方法,或只返回标记内的原始文本.我想要检索不是新列表或文本,而是检索标记内的源代码HTML.例如:
hxs.select('//span[@class="title"]/html()')
Run Code Online (Sandbox Code Playgroud)
我想做一个像这样的方法XPathSelectorList,shal返回它里面的HTML,像这样:
<html>
<head>
<title></title>
</head>
<body>
<div id="leexample">
justtext
<p class="ihatelookingforfeatures">
sometext
</p>
<p class="yahc">
sometext
</p>
</div>
<div id="lenot">
blabla
</div>
an awfuly long example for this.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我希望我清除了围绕我的问题的模棱两可.
如何从Scrapy中的HtmlXPathSelector获取HTML?(也许解决方案外部scrapy的范围?)