在用Scrapy制作蜘蛛的同时,我遇到了一些我似乎无法克服的事情。
for quote in response.css('div.entry-content'):
yield {
'title': quote.css('h3.widget-title::text').extract_first(),
'text': quote.css('div p::text').extract_first(),
这是我要提取的内容,因此从p
获取title
和div.panel-layout
,但p
到达strong
后或者说p
内的任何内容,该页面的抓取结束。
给出一个(文本)示例
The class **LocalTime**
之后,蜘蛛在遇到 LocalTime 作为strong
后结束活动。
我尝试添加p::text strong::text
,但似乎并没有解决它。我将如何解决所述问题,建议和提示不仅仅是值得赞赏的。
Edit1:我已从::text
移除p::text
部分解决了问题,但现在它为我提供了内容中的所有内容的代码,如下所示<p> The class <strong>LocalTime</strong> ... </p>
答案 0 :(得分:1)
我不知道我是否理解正确。 你希望刮掉没有'strong'标签的'p'标签? 如果是这样,那是不可能的 - 您应该使用正则表达式将其删除。 像这样:
re.findall(r'>(.+?)<', "<p> The class <strong>LocalTime</strong> ... </p>")
答案 1 :(得分:1)
你可以使用这样的东西(XPath string()
函数):
for quote in response.xpath('//div[@class="entry-content"]'):
yield {
'title': quote.xpath('./h3[@class="widget-title"]/text()').extract_first(),
'text': quote.xpath('string(./div/p)').extract_first(),
}