Не удается найти некоторые тексты, расположенные вне целевых элементов - PullRequest
1 голос
/ 23 апреля 2019

Я написал сценарий в scrapy, чтобы получить ответы на различные вопросы с веб-страницы. Проблема в том, что ответы находятся за пределами элементов, на которые я сейчас нацеливаюсь. Я знаю, что мог бы схватить их, используя .next_sibling, если бы использовал BeautifulSoup, но в случае скрапа я не могу найти никакой идеи.

ссылка на сайт

Элементы HTML похожи на:

  <p>
   <b>
    <span class="blue">
     Q:1-The NIST Information Security and Privacy Advisory Board (ISPAB) paper "Perspectives on Cloud Computing and Standards" specifies potential advantages and disdvantages of virtualization. Which of the following disadvantages does it include?
    </span>
    <br/>
    Mark one answer:
   </b>
   <br/>
   <input name="quest1" type="checkbox" value="1"/>
   It initiates the risk that malicious software is targeting the VM environment.
   <br/>
   <input name="quest1" type="checkbox" value="2"/>
   It increases overall security risk shared resources.
   <br/>
   <input name="quest1" type="checkbox" value="3"/>
   It creates the possibility that remote attestation may not work.
   <br/>
   <input name="quest1" type="checkbox" value="4"/>
   All of the above
  </p>

Я уже пробовал с:

import requests
from scrapy import Selector

url = "https://www.test-questions.com/csslp-exam-questions-01.php"

res = requests.get(url,headers={"User-Agent":"Mozilla/5.0"})
sel = Selector(res)
for item in sel.css("[name^='quest']::text").getall():
    print(item)

Приведенный выше сценарий ничего не печатает, когда его ожидают, он также не выдает ошибки.

Один из ожидаемых выходных данных из вставленных выше HTML-элементов:

It initiates the risk that malicious software is targeting the VM environment.

Я только после любого решения селектора CSS.

Как я могу получить ответы на разные вопросы с этого сайта?

Ответы [ 3 ]

1 голос
/ 29 апреля 2019

Следующая комбинация простых селекторов CSS и функций списка Python может решить эту задачу:

import scrapy
from scrapy.crawler import CrawlerProcess

class QuestionsSpider(scrapy.Spider):
    name = "TestSpider"
    start_urls = ["https://www.test-questions.com/csslp-exam-questions-01.php"]

    def parse(self,response):
    #select <p> tag elements with questions/answers
        questions_p_tags = [ p for p in response.css("form p")
                             if '<span class="blue"' in p.extract()]
        for p in questions_p_tags:
    #select question and answer variants inside every <p> tag
            item = dict()
            item["question"] = p.css("span.blue::text").extract_first()
    #following list comprehension - select all text, filter empty text elements
    #and select last 4 text elements as answer variants
            item["variants"] = [variant.strip() for variant in p.css("::text").extract() if variant.strip()][-4:]
            yield item

if __name__ == "__main__":
    c = CrawlerProcess({'USER_AGENT':'Mozilla/5.0'})
    c.crawl(QuestionsSpider)
    c.start()
0 голосов
/ 23 апреля 2019

Вы не можете сделать это в данный момент, используя только CSS.

cssselect , лежащая в основе библиотека response.css(), не поддерживает выбор одноуровневого текста.

Самое большее, вы можете выбрать первый следующий элемент:

>>> selector.css('[name^="quest"] + *').get()
'<br>'
0 голосов
/ 23 апреля 2019

Вы можете попытаться получить текст после тегов как following-sibling::text().Проверьте этот пример:

>>> sel.css("[name^='quest']").xpath('./following-sibling::text()').extract()
[u'\n   It initiates the risk that malicious software is targeting the VM environment.\n   ', u'\n   ', u'\n   It increases overall security risk shared resources.\n   ', u'\n   ', u'\n   It creates the possibility that remote attestation may not work.\n   ', u'\n   ', u'\n   All of the above\n  ', u'\n   It increases overall security risk shared resources.\n   ', u'\n   ', u'\n   It creates the possibility that remote attestation may not work.\n   ', u'\n   ', u'\n   All of the above\n  ', u'\n   It creates the possibility that remote attestation may not work.\n   ', u'\n   ', u'\n   All of the above\n  ', u'\n   All of the above\n  ']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...