提取scrapy中的标签值

时间:2019-01-25 18:15:09

标签: python-2.7 web-scraping scrapy

我想在scrapy fpr示例中提取xpath标签的值,

/html/body/div[3]/ul[1]/li[1]/div/p

q1

/html/body/div[3]/ul[1]/li[3]/div/p

ans1

/html/body/div[3]/ul[2]/li[1]/div/p

q2

/html/body/div[3]/ul[2]/li[2]/div/p

ans2 链接:https://www.digikala.com/ajax/product/questions/980291

以这样的产量

 def parse(self, response):
        for quote in response.xpath('//html/body/main'):
            yield {
#question or answer 
#question pattern  li/div/p  or li[1]/div/p
#answer pattern ended with li[2 or higher number]/div/p
#related question and answer both have the same ul for example both are ul[1]
                'type': quote.xpath('i dont know this part').extract_first (),
                'QAnumber': quote.xpath('?').extract(),
                'text': quote.xpath('/html/body/div[3]/*/*/div/p/text()').extract(),


            }

我该如何提取这三个部分

2 个答案:

答案 0 :(得分:1)

 def parse(self, response):
     for quote in response.css('#product-questions-list > ul'):
         quest = response.css('.is-question > div.section > div > p::text').extract_first()
         answer = response.css('.is-answer > div.section > div > p::text').extract_first()
         yield {quest: answer}

答案 1 :(得分:0)

很难理解您的问题。您是否要提取问题和答案?会是这样。

from w3lib.html import remove_tags
for qa in response.css('div#product-questions-list ul.c-faq__list'):
    question = qa.css('li.is-question div.section > p::text').get()
    answer = qa.css('li.is-answer div.section > p').get()
    answer = remove_tags(answer) if answer else None
    number = qa.css('li.is-question a::attr(data-question-id)')