【问题标题】:scrapy encoding data text pythonscrapy编码数据文本python
【发布时间】:2019-03-06 00:28:59
【问题描述】:

我需要你们帮助大家,抓取一个加密的文本元素 这是我的蜘蛛

      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}

                    )

我的问题是返回的电话字符串已编码,需要您帮助获取文本 提前谢谢!

【问题讨论】:

    标签: python scrapy


    【解决方案1】:
    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})
    

    【讨论】:

    • 嗨@yash pokar,非常感谢你做我一直在寻找的事情,你拯救了我的一天,我只是有一个问题,我怎样才能得到以其他数字开头的数字电话 06 , 我看到爬取不报废数字以 09 或 01 开头的问候再次感谢您
    • @AbdelmoulaNami 只是把这个条件放在产量项之前
    • 它工作正常,你知道我怎样才能让它进入下一页吗?
    • next_page = response.xpath('//*[@rel="next"]').extract_first()
    • @AbdelmoulaNami 现在安排下一页请求
    【解决方案2】:

    似乎该网站正在使用他们自己的内部 AJAX 调用来解密电话号码字符串;如果您查看您的网络浏览器检查器:

    你可以在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))
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多