使用Scrapy进行转义的元素(AJAX调用?)

时间:2019-04-19 09:02:40

标签: python ajax scrapy

我尝试抓取一个网站。我拥有所有URL,但是当抓取开始时,我会显示以下消息:

2019-04-19 09:54:01 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (301) to <GET http://www.leparking.fr/> from <GET http://www.leparking.fr/?_escaped_fragment_=%2Fvoiture-occasion%2FAC-12-HP.html%253Fslider_millesime%253D1920%257C1928%2526tri%253Ddate%2526slider_prix%253D500%257C400000>

基本URL为:http://www.leparking.fr/#!/voiture-occasion/AC-12-HP.html%3Fslider_millesime%3D1920%7C1928%26tri%3Ddate%26slider_prix%3D500%7C400000

我猜有一个错误,因为“#!”与AJAX有关。 结果,我所有的网址都是假的。但是我怎么能在Scrapy中刮它们呢?

这是我的蜘蛛代码示例:

import json
import scrapy
import re
import pkgutil

from scrapy.loader import ItemLoader
from annonces_spiders.items import AnnonceItem

class GlenMarchSpider(scrapy.Spider):
    name = 'annonces_results'

    def __init__(self, *args, **kwargs):
        data_file = pkgutil.get_data("annonces_spiders", "json/input/db_scrap_url_lp_js_10000_reduced.json")
        self.data = json.loads(data_file)


    def start_requests(self):
        for item in self.data:
            request = scrapy.Request(item['url_lp'], callback=self.parse)
            request.meta['item'] = item
            yield request

    def parse(self, response):
        item = response.meta['item']
        item['results'] = []

        for caritem in response.css("li.li-result"):
            data = AnnonceItem()
            data["marque"] = caritem.css("span.brand::text").extract_first().capitalize().replace(" ", "").strip()
            data["model"] = caritem.css("span.sub-title::text").extract_first().capitalize().replace(" ", "").strip()
            data["model_year"] = caritem.css("li:nth-child(3) > div.upper::text").extract_first().replace(" ", "").strip()
            data["price_str"] = caritem.css("p.prix::text").extract_first().strip()
            if "NC" not in data["price_str"]:
                data["price_int"] = int(caritem.css("p.prix::text").extract_first().replace("€", "").replace(" ", "").strip())
            else:
                data["price_int"] = None
            data["annonce_date"] = caritem.css("p.btn-publication::text").extract_first().strip().replace("/", "-")
            data["country"] = caritem.css("div.location > img::attr(alt)").extract_first().capitalize().strip()

            item['results'].append(data)

        yield item

0 个答案:

没有答案