Scrapy-试图从另一个标签内部获取标签文本

时间:2018-10-22 14:53:04

标签: python scrapy

我试图将所有东西都包含在p标签中,包括嵌套的b标签,但是我却得到了除b之外的所有其他东西。有人可以帮我吗?

这是我的代码:

def parse(self, response):
    images = response.css("#mw-content-text > div > table > tbody  > tr > td > a > img::attr(src)").extract_first()
    text1 = response.css("#mw-content-text > div > p::text").extract() 
    text2 = ''.join(text1)
    yield {'text2': text2, 'image_urls':[PROTOCOL+ images]}
    for next_page in response.css('#mw-content-text > div > ul > li > b > a::attr(href)').extract():
        yield Request(BASE_URL + next_page, callback=self.parse)

1 个答案:

答案 0 :(得分:1)

您必须在::text之前使用空格,以使选择器检索 all 最后一个标签下方的文本:

text1 = response.css("#mw-content-text > div > p ::text").extract() 
相关问题