Как получить данные Dynami c с помощью скрапа? - PullRequest
0 голосов
/ 05 апреля 2020

Привет всем. Я новичок в области психотерапии, и у меня возникли некоторые проблемы с парсингом одного из веб-сайтов.

Мне нужно получить данные с этой страницы: https://technopoint.ru/catalog/recipe/e351231ca6161134/2020-goda/

Мне нужно название модели, цена и ссылка на изображение. Я нашел все названия, кроме цены, и я почти разочаровался в этом.

Я нашел другую ссылку 'https://technopoint.ru/ajax-state/price/', но не могу понять, как извлечь из нее данные о ценах.

import scrapy
from scrapy.utils import curl

import json
from scrapy.http import FormRequest, Request
from scrapy.selector import Selector
from dateutil import parser
import re
import pandas as pd

class test(scrapy.Spider):
    name = 'prices'
    start_urls = ['https://technopoint.ru/catalog/recipe/e351231ca6161134/2020-goda/']
    price_url = 'https://technopoint.ru/ajax-state/price/'

    def parse(self, response):
        price_url = 'https://technopoint.ru/ajax-state/price/'
        params = {'type': 'price', 'containers': []}
        s = Selector(text=response.body)
        items = s.xpath('//div[@class="catalog-item"]').getall()
        f = open('p.html', 'w')
        for item in items[:10]:
            print(item, sep='\n', file=f)
        price_url = 'https://technopoint.ru/ajax-state/price/'
        req = scrapy.Request(price_url,
                             method='POST',
                             headers={'X-Requested-With': 'XMLHttpRequest',
                                      'Content-Type': 'application/x-www-form-urlencoded',
                                    },
                             callback=self.parse)
        print(response, file=open('output.html', 'w'))
        yield req

РЕДАКТИРОВАНИЕ

Я проверяю запрос от 'https://technopoint.ru/ajax-state/price/' и добавляю заголовки в мой scrapy.request, но все еще получаю эту ошибку 2020-04-05 22:11:34 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 https://technopoint.ru/ajax-state/price/>: HTTP status code is not handled or not allowed

Обновленный код:

import scrapy
from scrapy.utils import curl

import json
from scrapy.http import FormRequest, Request
from scrapy.selector import Selector
from dateutil import parser
import re
import pandas as pd

class test(scrapy.Spider):
    name = 'prices'
    start_urls = ['https://technopoint.ru/catalog/recipe/e351231ca6161134/2020-goda/']
    price_url = ')'

    def parse(self, response):
        params = {'type': 'price', 'containers': []}
        smartphones = [['Наименование', 'Артикул', 'Цена', 'Ссылка на картинку']]
        s = Selector(text=response.body)
        items = s.xpath('//div[@class="catalog-item"]').getall()
        f = open('p.html', 'w')
        for item in items[:10]:
            print(item, sep='\n', file=f)
        price_url = 'https://technopoint.ru/ajax-state/price/'
        req = Request(price_url,
                          method='POST',
                          headers={'authority:': 'technopoint.ru',
                                   'accept:': '*/*',
                                   'accept-encoding:': 'gzip, deflate, br',
                                   'accept-language:': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
                                   'cache-control:': 'max-age=0',
                                   'content-length:': '1980',
                                   'content-type:': 'application/x-www-form-urlencoded',
                                   'cookie:': 'PHPSESSID=0286ee880ac1d5cd57f43f2eeec0b73c; city_path=vladivostok; current_path=ad13a34f0ddacba4dcad66ac94887fdc394ece79337b20b5fc6d9f288565db3ca%3A2%3A%7Bi%3A0%3Bs%3A12%3A%22current_path%22%3Bi%3A1%3Bs%3A64%3A%22%7B%22city%22%3A%2230b7c1ea-03fb-11dc-95ee-00151716f9f5%22%2C%22method%22%3A%22geoip%22%7D%22%3B%7D; _csrf=8eb88749c6a678f660c971c1cefb8483f207a0ff20de8ca588505d3baec9d74ea%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22AymmzAHzm1SCdxa1NPybnEvX7p2A_qO6%22%3B%7D; cartUserCookieIdent_v3=e867130bc8d9f8937f0570c4484fc05a8a8974d4fa41153f4a2c2b0a7688e2b9a%3A2%3A%7Bi%3A0%3Bs%3A22%3A%22cartUserCookieIdent_v3%22%3Bi%3A1%3Bs%3A36%3A%22d177d2b6-cd1a-35ad-a5e8-56eb6755a7c7%22%3B%7D; orderCheckoutIdent=ee1b11953bcef61abd907cdcfe07e812e5fac4297be4b6eda7c204f421442b7ba%3A2%3A%7Bi%3A0%3Bs%3A18%3A%22orderCheckoutIdent%22%3Bi%3A1%3Bs%3A36%3A%22d177d2b6-cd1a-35ad-a5e8-56eb6755a7c7%22%3B%7D; ipp_uid2=tq5HZLPERsnOuc6w/0bg7KR9umEEG+g2hS7V5Lw==; ipp_uid1=1585731002688; ipp_uid=1585731002688/tq5HZLPERsnOuc6w/0bg7KR9umEEG+g2hS7V5Lw==; rerf=AAAAAF6EVb2sXQVLAwNyAg==; _ym_uid=1585731008897196727; _ym_d=1585731008; _ga=GA1.2.549879528.1585731008; wishlist-id=7d86b3b74562fd04ac0f58b36ae80013b7c5a3210bf695ae02947c52aab394eba%3A2%3A%7Bi%3A0%3Bs%3A11%3A%22wishlist-id%22%3Bi%3A1%3Bs%3A36%3A%22ce061855-0700-42b4-b911-3d5285495361%22%3B%7D; phonesIdent=1b41353e810b6fbdff1c5e04d5595226073a5894bc3e0672e6635a32638b74bca%3A2%3A%7Bi%3A0%3Bs%3A11%3A%22phonesIdent%22%3Bi%3A1%3Bs%3A36%3A%22e6ea120a-b4a9-41ae-b193-e6473a974355%22%3B%7D; viewed_products=f638336fc75eb418edbbb8352d30d78bfcff05baeae41df4d551e17e9d26b8cda%3A2%3A%7Bi%3A0%3Bs%3A15%3A%22viewed_products%22%3Bi%3A1%3Ba%3A10%3A%7Bi%3A0%3Bs%3A36%3A%226f312f05-6cc5-11ea-a20f-00155d03332b%22%3Bi%3A1%3Bs%3A36%3A%2239f97f62-6cc5-11ea-a20f-00155d03332b%22%3Bi%3A2%3Bs%3A36%3A%2253a625b4-6cc5-11ea-a20f-00155d03332b%22%3Bi%3A3%3Bs%3A36%3A%2283b41a2e-6cc4-11ea-a20f-00155d03332b%22%3Bi%3A4%3Bs%3A36%3A%22b8dccbec-6cc4-11ea-a20f-00155d03332b%22%3Bi%3A5%3Bs%3A36%3A%22a2071eb1-6cc4-11ea-a20f-00155d03332b%22%3Bi%3A6%3Bs%3A36%3A%22d1357546-6cc4-11ea-a20f-00155d03332b%22%3Bi%3A7%3Bs%3A36%3A%22def4e505-6cc3-11ea-a20f-00155d03332b%22%3Bi%3A8%3Bs%3A36%3A%22aca1c017-6cc3-11ea-a20f-00155d03332b%22%3Bi%3A9%3Bs%3A36%3A%221bb514cf-6cc4-11ea-a20f-00155d03332b%22%3B%7D%7D; _gid=GA1.2.1962929093.1585979094; _ym_isad=1; _ym_visorc_22425643=w; _gat=1',
                                   'origin:': 'https://technopoint.ru',
                                   'referer:': 'https://technopoint.ru/catalog/recipe/e351231ca6161134/2020-goda/no-referrer',
                                   'sec-fetch-dest:': 'empty',
                                   'sec-fetch-mode:': 'cors',
                                   'sec-fetch-site:': 'same-origin',
                                   'user-agent:': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
                                   'x-csrf-token:': '39b2EIZ9K76p1VapB2fDUf4jI_eLXwpQki8hkdVd4emer5t9_DxjxMTkBepjH6JgsHNaleUafAilXxPQiiyu3w==',
                                   'x-requested-with:': 'XMLHttpRequest'},
                          # formdata={"type": "", "containers": "[{'id': "", 'data': {'product': ""}}]"},
                          callback=self.parse)
        print(response, file=open('output.html', 'w'))
        yield req

1 Ответ

0 голосов
/ 05 апреля 2020

Вам необходимо проверить запрос, который вы упомянули, и отправить его с теми же параметрами в scrapy, используя класс Request.

Примерно так:

yield scrapy.Request("https://technopoint.ru/ajax-state/price/",
                     headers={'header', 'value'},
                     callback=self.parse_price
                    )

Подробнее о Request здесь

...