首页 > 解决方案 > 在单独的行中抓取问题和答案,例如 A1 列的第一行包含 que 1 第一行 A2 col catains answer for qes 1 等等

问题描述

在此处输入图片描述我想从以下网站https://www.sanfoundry.com/python-mcqs-basic-operators/抓取问卷

格式应使每个问题和答案都显示在不同的列中。如果问题只包含没有任何 html 标记的文本,那就更好了。

我使用scrapy来抓取这些信息,但我面临的问题是所有问题都出现在一行中,而所有答案都出现在另一行中。我需要显示为:问题 1 在一行中,该问题的相应答案在另一行中。

import scrapy

class QuestionSpider(scrapy.Spider):

    name = "redbot"

    start_urls = ['https://www.sanfoundry.com/python-mcqs-basic-operators/']

    def parse(self, response):
        #Extracting the content using xpath selectors
        text = [
            ' '.join(
                line.strip() 
                for line in response.css("div > p").extract_first()
                if line.strip()
            ) 
            for p in response.xpath('//p')
        ]

        votes = [
            ' '.join(
                line.strip() 
                for line in response.css(".collapseomatic_content ::text") .extract_first()
                if line.strip()
            ) 
            for p in response.xpath('//div')
        ]
       
   
        #Give the extracted content row wise
        for item in zip(text,votes):
            #create a dictionary to store the scraped info
            scraped_info ={
                'texts' : item[0],
                'vote' : item[1]   
            }

            #yield or give the scraped info to scrapy
            yield scraped_info

我需要使用scrapy代码在没有html标签的2个不同行中的每个问题和相应答案。请更正我提供的代码。

标签: pythonweb-scrapingscrapyweb-crawlerscrapy-shell

解决方案


推荐阅读