Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Scrapy中获取注释之间的内容_Python_Xpath_Comments_Scrapy_Block - Fatal编程技术网

Python 在Scrapy中获取注释之间的内容

Python 在Scrapy中获取注释之间的内容,python,xpath,comments,scrapy,block,Python,Xpath,Comments,Scrapy,Block,我试图通过Scrapy获得评论之间的内容。我已经找到了,但除了通过这种方式提取评论之外,我没有做更多的事情。不过,我的目标是每次出现开始评论和结束评论时,都能得到这两条评论之间的所有内容。假设网站上的块的结构如下: <!-- Start --> <div class.. > <ul>... more content </ul> </div> <!-- End --> <!-- Star

我试图通过Scrapy获得评论之间的内容。我已经找到了,但除了通过这种方式提取评论之外,我没有做更多的事情。不过,我的目标是每次出现开始评论和结束评论时,都能得到这两条评论之间的所有内容。假设网站上的块的结构如下:

<!-- Start -->
<div class.. >
    <ul>...
        more content
    </ul>
</div>
<!-- End -->

<!-- Start -->
    same structure, different entries
<!-- End -->

<!-- Start -->
    same structure, different entries
<!-- End -->
....
我要寻找的是类似xpath的东西,它可以查找特定字符串并将它们全部保存在一个列表中,并且会得到一个类似于[contentBlock1,contentBlocks2,…]的输出,就像通常使用xpath获得输出的方式一样。任何有助于您的方式都将受到高度赞赏:

编辑:可能值得注意的是,我目前正在处理一个scrapy响应/选择器对象。如果您愿意使用re,您可以尝试此方法

print re.findall(r"(?<=<!-- Start -->\s)(.*?)(?=\s<!-- End -->)",test_string,re.DOTALL)
这将返回一个包含所需内容的列表

见演示


类似于response.xpath'//*[@id=your id here]/ul/text'。提取应该可以做到这一点

块是从到的,所以你想要注释之间的所有html?是的,正是这样。我有一切可以过滤的内容后,但将需要块结构和块分别从它获得可靠的信息。评论至少要开始与开始或结束?那么其他不匹配的注释会被忽略吗?试试这个还不起作用,可能是因为我想知道如何识别块的结束位置。还有什么是id,因为没有直接设置id,或者scrapy允许您只设置一个comment=id?对不起,应该由//div[\@class='foo']匹配,所以将'@id'更改为'@class'。哦,我想当时还不够清楚。我真的必须按注释而不是类进行过滤,因为类名会更改和/或也会用于我不想使用的块。由于您具有相同的结构消息,我假设您的每个注释都具有相同的类名。你能发布一个真实的html示例吗?也许,这会有帮助:是的,我理解这种混淆,但我认为注释之间的混淆是足够清楚的:P在这里复制了一个示例:是的,我已经在试验re模块,这似乎很合逻辑。test_字符串变量的确切含义是什么?直接使用我从选择器中得到的东西在这里不起作用,当然,它不是一个真正的字符串。@Shin整个内容都必须是字符串。您对表单的输入是什么?我们可以将其转换为字符串吗?如前所述,我使用的是Scrapy。使用Selectorresponse返回,来自yield Requesturl、def的响应是。既没有字符串,也不确定以某种方式创建字符串是否有意义。我会调查的。