【问题标题】:Scrapy + Splash + ScrapyJSScrapy + Splash + ScrapyJS
【发布时间】:2016-03-03 18:57:42
【问题描述】:

我正在使用Splash 2.0.2 + Scrapy 1.0.5 + Scrapyjs 0.1.1,但我仍然无法通过单击呈现 javascript。这是一个示例网址https://olx.pt/anuncio/loja-nova-com-250m2-garagem-em-box-fechada-para-arrumos-IDyTzAT.html#c49d3d94cf

我仍然得到没有呈现电话号码的页面:

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()

我怎样才能让它工作?

【问题讨论】:

    标签: python scrapy screen-scraping scrapy-spider


    【解决方案1】:

    添加

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

    到 Lua 脚本,它会工作。

    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() 是 JQuery 函数https://api.jquery.com/click/

    【讨论】:

      【解决方案2】:

      您可以避免首先使用Splash 并发出适当的GET 请求来自己获取电话号码。工作蜘蛛:

      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)
      

      如果要从这个“动态”网站中提取更多内容,请查看 Splash 是否真的足够,如果没有,请查看浏览器自动化和selenium

      【讨论】:

      • 我实际上需要它来工作,因为我将转移到带有日期选择器日历和东西的更复杂的 js 网站
      • @psychok7 你确定scrapyjs 足以满足你复杂的动态网站吗?也许切换到selenium 会让事情变得更快更简单..
      • 我正在尝试..我不知道它是否可能..但我也会研究硒谢谢
      • @psychok7 好的,在答案中添加了关于 selenium 的注释。很抱歉没有解决您的 Splash 特定问题,但我个人会通过 selenium.. 解决这个问题,可能部分是因为我比 Splash 更熟悉它,但我的印象是 Splash 不会普遍解决“动态性”问题作为一个真正的浏览器会......只是一个想法......
      • 我接受了你的回答,因为我看到 selenium 更加成熟,但我遇到了一些问题,也许你可以帮助我?这是我的问题stackoverflow.com/questions/35799855/scrapy-selenium-datepicker
      猜你喜欢
      • 2016-06-13
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2017-12-23
      • 2018-01-08
      • 2021-12-26
      • 2020-05-04
      • 1970-01-01
      相关资源
      最近更新 更多