代码之家  ›  专栏  ›  技术社区  ›  LucSpan

Href在scrapy结果中不可见,但在html中可见

  •  1
  • LucSpan  · 技术社区  · 6 年前

    组织

    我有来自的下一页按钮元素 this page ,

    <li class="Pagination-item Pagination-item--next  Pagination-item--nextSolo ">
                            <button type="button" class="Pagination-link js-veza-stranica kist-FauxAnchor" data-page="2" data-href="https://www.njuskalo.hr/prodaja-kuca?page=2" role="link">Sljedeća&nbsp;<span aria-hidden="true" role="presentation">»</span></button>
                        </li>
    

    data-href


    代码

    response.xpath('//*[@id="form_browse_detailed_search"]/div/div[1]/div[5]/div[1]/nav/ul/li[8]/button').extract_first()                        
    

    我找回,

    '<button type="button" class="Pagination-link js-veza-stranica" data-page="2">Sljedeća\xa0<span aria-hidden="true" role="presentation">»</span></button>'
    

    问题:

    火车在哪里 数据href

    1 回复  |  直到 4 年前
        1
  •  1
  •   Valdir Stumm Junior    6 年前

    data-href

    无论如何,解决这个问题的一种方法是基于 data-page 属性:

    from w3lib.url import add_or_replace_parameter
    ...
    
    next_page = response.css('.Pagination-item--nextSolo button::attr(data-page)').get()
    next_page_url = add_or_replace_parameter(response.url, 'page', next_page)
    

    w3lib 是一个开源库: https://github.com/scrapy/w3lib