Scrapy:如何通过AJAX调用刮取第二个HTML页面

问题描述 投票:0回答:1

我是scrapy和html的新手,我正在尝试创建一个简单的蜘蛛来刮掉https://www.mobiel.nl网站。

我设法访问了移动电话页面,例如https://www.mobiel.nl/smartphone/apple/iphone-6-32gb

我正在尝试获取有关计划的信息,例如运营商名称(取自图像名称),计划名称和费率,这些信息存储在以下容器中:

<div class="pc-result js-offer" data-offer-id="71-1928-3683-19.0">

我已经尝试了几种不同的方式来扭转选择器,例如:

 scrapy shell https://www.mobiel.nl/smartphone#
 fetch('https://www.mobiel.nl/smartphone/apple/iphone-6-32gb') 

In [37]: response.xpath('//*[@id="js-compare-results"]/text()')
Out[37]: []

In [38]: response.xpath('//*[@id="js-compare-results"]/*')
Out[38]: []

In [39]: response.xpath('//*[@id="js-compare-results"]')
Out[39]: []

In [40]: response.xpath('//*[@id="js-compare-results"]/div/div[2]/div[2]/div/div[1]/div/div[1]/div[1]/span[1]')
Out[40]: []

In [41]: response.xpath('//*[@id="js-compare-results"]/div/div[2]/div[2]/div/div[1]/div/div[1]/div[1]/span[1]').extract()
Out[41]: []

我无法找到获取任何信息的方法,除了设备名称,即:response.xpath('//*[@class="phone-info__phone"]/text()').extract_first()

最后我想有类似的东西

[device name, operator (e.g. t-mobile), plan (e.g. 1GB), period (e.g. 1 year) rate (e.g. 15€)]

有谁知道如何从此页面正确提取(如果可能)此类信息?

先感谢您。

**Edit 1: spider sourcecode**

    # -*- coding: utf-8 -*-
from scrapy import Spider
from scrapy.http import Request
from scrapy_splash import SplashRequest
import re

class TmnlPricecrawlerSpider(Spider):
    name = 'tmnl_pricecrawler'
    allowed_domains = ['www.mobiel.nl']
    start_urls = ['https://www.mobiel.nl/smartphone#']

    def parse(self, response):
        #Process spartphone pages - for this website, all phones are in the same page, no multi-pages processing needed
        mobielnl_items = response.xpath('//*[@class="phone-list-item__link"]/@href').extract()
        for item in mobielnl_items:
            item_url = response.urljoin(item)
            yield Request(item_url, callback=self.parse_mobielnl)

            #for url in item_url:
                #yield SplashRequest(url=url, callback=self.parse_mobielnl)


    def parse_mobielnl(self, response):
        yield SplashRequest(url=url, callback=self.parse_aaa)

    def parse_aaa():
        pass

我尝试使用scrapy_splash获取内部URL但仍然没有成功。

编辑2:我意识到:

In [87]: response.xpath('//*[@id="price-comparator"]').extract_first()
Out[87]: '<div id="price-comparator" class="page-width page-width--spacing" data-style="mobielnl" data-token="EnsjtkLMsBkkYyLQVEZwqA" data-phone="803"></div>'

<div id="price-comparator" class="page-width page-width--spacing" data-style="mobielnl" data-token="EnsjtkLMsBkkYyLQVEZwqA" data-phone="803"><iframe src="https://pcnltelecom.tdsapi.com/portal/iframe/full_compare/?api_token=EnsjtkLMsBkkYyLQVEZwqA&amp;api_domain=https%3A%2F%2Fwww.mobiel.nl&amp;dom_id=price-comparator&amp;iframe_options[style]=mobielnl&amp;iframe_options[click_outs_in_parent]=true&amp;iframe_options[show_sponsored_positions]=false&amp;iframe_options[filter][phones][]=803&amp;iframe_options[type_options][phone_offers][show]=false&amp;iframe_options[type_options][propositions][show]=true&amp;iframe_options[type_options][sim_only][show]=false" width="100%" scrolling="no" frameborder="0" class="pc-iframe" id="iFrameResizer0" style="overflow: hidden; min-height: 500px; height: 1240.1px;"></iframe></div>

enter image description here

项目数据令牌和数据电话将这些数字提供给我需要的数据点的URL,这样就可以尝试获取此信息并将其替换为网址,或者是否有更充分的信息做这样的事情的方式?

python html scrapy web-crawler
1个回答
1
投票

如果您使用Chrome DevTools检查上述网址,您会发现通过单独的AJAX调用this URL请求此信息

这就是你的XPath表达式不起作用的原因。

© www.soinside.com 2019 - 2024. All rights reserved.