Почему scrapy Xpath не может найти то, что обнаружил мой браузер (ы) Xpath? - PullRequest
0 голосов
/ 09 февраля 2019

Я хочу найти что-то по Xpath на странице (первый проект Scrapy), например на странице https://github.com/rg3/youtube-dl/pull/11272.

И в моей Opera inspect, и в firefox TryXpath дополнениеэто выражение Xpath имеет тот же результат:

// div [@ class = 'файл js-комментарий-контейнер js-resolvable-timeline-thread-container has-inline-notes']

и это так:

enter image description here

НО в Scrapy 1.6 Xpath, когда я хочуполучить его результат, он не найдет ничего и просто вернет пустой список

 def parse(self, response):
    print(response.xpath('''//div[@class='file js-comment-container js-resolvable-timeline-thread-container has-inline-notes']'''))

, а результат будет просто [].

В чем, по-вашему, проблема?и как я могу это исправить?заранее спасибо.

ПРИМЕЧАНИЕ: да, я знаю о robot.text и даже ROBOTSTXT_OBEY = False

1 Ответ

0 голосов
/ 09 февраля 2019

Казалось бы, некоторые из этих классов добавляются с помощью javascript.
Однако, если вы сможете найти подходящий селектор, вы все равно сможете выбрать элементы, на которые вы пытаетесь нацелиться, дажеесли JavaScript не выполняется:

>>> fetch('https://github.com/rg3/youtube-dl/pull/11272')
2019-02-09 14:50:19 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://github.com/rg3/youtube-dl/pull/11272> (refere
r: None)
>>> response.css('div.file')
[<Selector xpath="descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' file ')]" dat
a='<div class="file js-comment-container js'>, <Selector xpath="descendant-or-self::div[@class and contains(concat(' ',
normalize-space(@class), ' '), ' file ')]" data='<div class="file js-comment-container js'>, <Selector xpath="descendant
-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' file ')]" data='<div class="file js-comme
nt-container js'>, <Selector xpath="descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '
), ' file ')]" data='<div class="file js-comment-container js'>, <Selector xpath="descendant-or-self::div[@class and con
tains(concat(' ', normalize-space(@class), ' '), ' file ')]" data='<div class="file js-comment-container js'>, <Selector
 xpath="descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' file ')]" data='<div cl
ass="file js-comment-container js'>, <Selector xpath="descendant-or-self::div[@class and contains(concat(' ', normalize-
space(@class), ' '), ' file ')]" data='<div class="file js-comment-container js'>, <Selector xpath="descendant-or-self::
div[@class and contains(concat(' ', normalize-space(@class), ' '), ' file ')]" data='<div class="file js-comment-contain
er js'>, <Selector xpath="descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' file
')]" data='<div class="file js-comment-container js'>]
>>> len(_)
9
...