Scrapy кодирование данных текст Python - PullRequest
0 голосов
/ 30 сентября 2018

Мне нужно, чтобы вы помогли, чтобы зачистить текстовый элемент, который зашифрован здесь, это мой паук

      import json
import scrapy


class YPSpider(scrapy.Spider):
    name = 'yp'
    start_urls = ['https://www.infobel.com/fr/france/business/50000/informatique_internet/']

    def parse(self, response):
    next_page = response.xpath('//*[@rel="next"]').extract_first()
    if next_page_url:
        yield response.follow(next_page_url, callback=self.parse)

    if response.meta.get('has_phone'):
        item = response.meta['item']

        response = json.loads(response.body)
        item['phone'] = response['result']

        yield item
    else:
        items = response.xpath('//*[contains(@class, "customer-box")]')

        for item in items:
            address_lines = item.xpath('.//span[contains(@class, "fa-map-marker")]/../span[@class="detail-text"]//text()').extract()

            title = item.xpath('.//h2[@class="customer-item-name"]/a/text()').extract_first().strip()
            address = address_lines[0].replace('\r', '').replace('\t', '').strip() if address_lines else ''
            village = address_lines[1].replace('\r', '').replace('\t', '').strip() if len(address_lines) >= 1 else ''
            phone = item.xpath('.//span[contains(@class, "icon-phone")]/../span[@class="detail-text"]/text()').extract()

            item = {
                'title': title,
                'address': address,
                'village': village,
                'phone': phone,
            }

            if phone:
                if phone[0].isnumeric():
                    item['phone'] = phone[0]

                    yield item
                elif len(phone) >= 2:
                    yield scrapy.Request('https://www.infobel.com/fr/france/Search/Decrypt?encryptedString={}'.format(phone[1]), meta={'item': item, 'has_phone': True}

                    )

Моя проблема в том, что возвращенная телефонная строка закодирована, и вам нужна помощь, чтобы получить текст. Спасибозаранее!

Ответы [ 2 ]

0 голосов
/ 01 октября 2018
import json
import scrapy


class YPSpider(scrapy.Spider):
    name = 'yp'
    start_urls = ['http://www.infobel.com/fr/france/business/50000/informatique_internet/']

    def parse(self, response):

        pages = response.xpath('//ul[@class="pagination"]//*[@rel="next"]/@href').extract()

        next_page = pages[-1] if pages else None

        if next_page:
            yield response.follow(next_page)


        if response.meta.get('has_phone'):
            item = response.meta['item']

            response = json.loads(response.body)
            item['phone'] = response['result']

            yield item
        else:
            items = response.xpath('//*[contains(@class, "customer-box")]')

            for item in items:
                address_lines = item.xpath('.//span[contains(@class, "fa-map-marker")]/../span[@class="detail-text"]//text()').extract()

                title = item.xpath('.//h2[@class="customer-item-name"]/a/text()').extract_first().strip()
                address = address_lines[0].replace('\r', '').replace('\t', '').strip() if address_lines else ''
                village = address_lines[1].replace('\r', '').replace('\t', '').strip() if len(address_lines) >= 1 else ''
                phone = item.xpath('.//span[contains(@class, "icon-phone")]/../span[@class="detail-text"]/text()').extract()

                item = {
                    'title': title,
                    'address': address,
                    'village': village,
                    'phone': phone,
                }

                if phone:
                    if phone[0].isnumeric():
                        item['phone'] = phone[0]

                        yield item
                    elif len(phone) >= 2:
                        yield scrapy.Request('https://www.infobel.com/fr/france/Search/Decrypt?encryptedString={}'.format(phone[1]), meta={'item': item, 'has_phone': True})
0 голосов
/ 01 октября 2018

Похоже, что веб-сайт использует собственные внутренние вызовы AJAX для расшифровки строк телефонных номеров;если вы посмотрите на своего веб-браузера инспектора: enter image description here

Вы можете повторить этот запрос в scrapy:

from urllib.parse import quote
from scrapy import Request

def parse(self, response):
    code = quote('iHB/1oF0m7ELfO6Mfsl+mvm+o8SZZ37q', safe='')
    url = f"https://www.infobel.com/fr/france/Search/Decrypt?encryptedString={code}"
    yield Request(url, body=json.dumps(data))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...