我对scrapy和html并不陌生,我正在尝试创建一个简单的Spider来抓取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&api_domain=https%3A%2F%2Fwww.mobiel.nl&dom_id=price-comparator&iframe_options[style]=mobielnl&iframe_options[click_outs_in_parent]=true&iframe_options[show_sponsored_positions]=false&iframe_options[filter][phones][]=803&iframe_options[type_options][phone_offers][show]=false&iframe_options[type_options][propositions][show]=true&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>
数据令牌和数据电话项将这些数字提供给请求我需要的数据点的URL,因此这将是尝试获取此信息并将其替换为url的方式另一种更合适的方式来做这样的事情?
答案 0 :(得分:1)
如果您使用Chrome DevTools查看上述URL,则会发现通过分别向this URL的AJAX调用来请求此信息
这就是为什么您的XPath表达式不起作用的原因。