Значение Scrapy Pass с использованием мета никогда не обновляется - PullRequest
0 голосов
/ 19 января 2020

Я пытался очистить некоторые сайты. Я уже получил данные и попытался передать значение, используя meta={}. Но проблема возникает, когда я использую yield scrapy.Request, которая переходит к следующей функции. Я был отправлен на следующую функцию, новый URL и с помощью meta для передачи значения JSON. Я получил новый URL, но не с данными JSON, JSON никогда не обновлялся. Просто передал те же значения. Я понятия не имею, что происходит.

Вы можете видеть мой код, я пытался передать значения JSON, но я получил только тот же JSON, но URL обновляется новым URL.

def first_function(self, response):
    value_json = self.get_json() #i got the json from here
    for key, value in value_json.items(): #loop the json
        for values in value:

            # values will show me as dictionay of dictionary
            # thats like {"blabla":{"key1":"value1","key1":"value2"}}
            # I gave the conditions as below to get just a value or to make sure if that key ("blabla") is exist
            # if the condition is true, i will get the value {"key1":"value1","key1":"value2"}

            if values == "blabla":
                get_url = "http://www.example.com/"
                yield Request(
                    url=get_url+values["id_url"],
                    meta={"data_rest":values}, 
                    callback=self.second_function
                )

def second_function(self, response):
    # ============== PROBLEM =====================
    # The problem is here!
    # I always got a new url from first_function, my logic is if I got a new url, i should get a new json
    # but the json or "data_rest" never updated. Always send the same json to this function
    # ============== PROBLEM =====================

    josn_data = response.meta["data_rest"]
    names = response.css() #get the tag in here
    for get_data in names:
        sub_url = get_data.css("a::attr(href)").extract()
        for loop_url_menu in sub_url:
            yield scrapy.Request(
                url=loop_url_menu, 
                headers=self.session,
                meta = {
                    'dont_redirect': True,
                    'handle_httpstatus_list': [302]
                }, callback=self.next_function
            )

1 Ответ

0 голосов
/ 31 января 2020

Хорошие новости !! Я могу решить это. нам просто нужно добавить значение для первого, после чего мы получим значение.

def first_function(self, response):
    temp = []
    value_json = self.get_json() #i got the json from here
    for key, value in value_json.items(): #loop the json
        for values in value:
            if values == "blabla":
                get_url = "http://www.example.com/"
                temp.append(values)

    yield Request(
        url=get_url+values["id_url"],
        meta={"data_rest":temp}, 
        callback=self.second_function
    )
...