2016-03-03 16 views
5

sto usando Splash 2.0.2 + Scrapy 1.0.5 + Scrapyjs 0.1.1 e non riesco ancora a visualizzare javascript con un clic. Ecco un esempio url https://olx.pt/anuncio/loja-nova-com-250m2-garagem-em-box-fechada-para-arrumos-IDyTzAT.html#c49d3d94cfScrapy + Splash + ScrapyJS

ancora sto ottenendo la pagina senza il numero di telefono reso:

class OlxSpider(scrapy.Spider): 
    name = "olx" 
    rotate_user_agent = True 
    allowed_domains = ["olx.pt"] 
    start_urls = [ 
     "https://olx.pt/imoveis/" 
    ] 

    def parse(self, response): 
     script = """ 
     function main(splash) 
      splash:go(splash.args.url) 
      splash:runjs('document.getElementById("contact_methods").getElementsByTagName("span")[1].click();') 
      splash:wait(0.5) 
      return splash:html() 
     end 
     """ 
     for href in response.css('.link.linkWithHash.detailsLink::attr(href)'): 
      url = response.urljoin(href.extract()) 
      yield scrapy.Request(url, callback=self.parse_house_contents, meta={ 
       'splash': { 
        'args': {'lua_source': script}, 
        'endpoint': 'execute', 
       } 
      }) 

     for next_page in response.css('.pager .br3.brc8::attr(href)'): 
      url = response.urljoin(next_page.extract()) 
      yield scrapy.Request(url, self.parse) 

    def parse_house_contents(self, response): 

     import ipdb;ipdb.set_trace() 

come posso ottenere questo lavoro?

risposta

2

È possibile evitare di dover utilizzare Splash in primo luogo e effettuare la richiesta GET appropriata per ottenere il numero di telefono. ragno di lavoro:

import json 
import re 

import scrapy 

class OlxSpider(scrapy.Spider): 
    name = "olx" 
    rotate_user_agent = True 
    allowed_domains = ["olx.pt"] 
    start_urls = [ 
     "https://olx.pt/imoveis/" 
    ] 

    def parse(self, response): 
     for href in response.css('.link.linkWithHash.detailsLink::attr(href)'): 
      url = response.urljoin(href.extract()) 
      yield scrapy.Request(url, callback=self.parse_house_contents) 

     for next_page in response.css('.pager .br3.brc8::attr(href)'): 
      url = response.urljoin(next_page.extract()) 
      yield scrapy.Request(url, self.parse) 

    def parse_house_contents(self, response): 
     property_id = re.search(r"ID(\w+)\.", response.url).group(1) 

     phone_url = "https://olx.pt/ajax/misc/contact/phone/%s/" % property_id 
     yield scrapy.Request(phone_url, callback=self.parse_phone) 

    def parse_phone(self, response): 
     phone_number = json.loads(response.body)["value"] 
     print(phone_number) 

Se ci sono più cose da estrarre da questo sito "dinamico", vediamo se Splash è davvero sufficiente e, se non, esaminare l'automazione browser e selenium.

+0

realtà ho bisogno di questo per lavorare perché mi trasferirò a più complessi siti js con calendari data picker e roba – psychok7

+1

@ psychok7 si è sicuri scrapyjs sarebbe sufficiente per il tuo complesso sito web dinamico? Forse passare a 'selenium' renderebbe le cose andare più veloci e più semplici .. – alecxe

+0

Lo sto provando .. Non ho idea se sia possibile o meno .. Ma guarderò anche il selenio grazie – psychok7

3

Aggiungere

splash:autoload("https://code.jquery.com/jquery-2.1.3.min.js") 

script Lua e funzionerà.

function main(splash) 
    splash:go(splash.args.url) 
    splash:autoload("https://code.jquery.com/jquery-2.1.3.min.js") 
    splash:runjs('document.getElementById("contact_methods").getElementsByTagName("span")[1].click();') 
    splash:wait(0.5) 
    return splash:html() 
end 

.Click() è la funzione JQuery https://api.jquery.com/click/