(Scrapy)无法从html中提取HREF

时间:2017-05-03 03:50:46

标签: python xpath css-selectors scrapy

next_page = response.css('body > div.bg-theme-b.page-results.page-listing-results.page-results-responsive.ui-page.ui-page-theme-a.ui-page-header-fixed.ui-page-active > div.ui-panel-wrapper > div > div:nth-child(10) > div > ul > li.ui-block-b > a').extract_first()

    if next_page is not None:
        next_page = response.urljoin(next_page)
        yield scrapy.Request(next_page, callback=self.parse)

我正试图从m.zoopla.co.uk(移动,因为它似乎更好地从...抓取数据)的房价数据。我无法获得scrapy来获取下一页链接上的href属性。数据从第一页上删除很好 - 没有问题。

我使用了这里显示的最长,最直接的CSS选择器 - 它不起作用。我尝试使用较短的选择器:response.css('li.ui-block-b > a::attr(href)').extract_first(),它仍然返回为无。

我也尝试过使用XPath。那里也没有雪茄。请帮忙。有人。任何人..

PS。只包含下一页的代码,因为没有错误,实际的数据抓取工作正常。

1 个答案:

答案 0 :(得分:0)

您只需选择具有<a>属性的data-icon="carat-r"元素,然后获取href,因为只有下一页链接具有属性data-icon="carat-r"

next_page = response.css('a[data-icon*=carat-r]::attr(href)').extract_first()

<强>输出:

2017-05-03 13:27:53 [quotes] DEBUG: next_page is /house-prices/browse/coalville/?pn=2
相关问题