【问题标题】:How to get href from the entire page with scrapy (proper css selector)?如何使用scrapy(正确的css选择器)从整个页面获取href?
【发布时间】:2020-09-08 15:42:56
【问题描述】:

我正在尝试抓取一个房地产网站:https://www.nepremicnine.net/oglasi-prodaja/slovenija/hisa/。我想获取隐藏在房屋图像标签中的 href:

我想为整个页面(和其他页面)获取此信息。这是我编写的不返回任何内容的代码(例如空字典):

import scrapy
from ..items import RealEstateSloItem
import time

# first get all the URLs that have more info on the houses
# next crawl those URLs to get the desired information

class RealestateSpider(scrapy.Spider):
    # allowed_domains = ['nepremicnine.net']
    name = 'realestate'
    page_number = 2
    # page 1 url
    start_urls = ['https://www.nepremicnine.net/oglasi-prodaja/slovenija/hisa/1/']

    def parse(self, response):

        items = RealEstateSloItem()  # create it from items class --> need to store it down

        all_links = response.css('a.slika a::attr(href)').extract()

        items['house_links'] = all_links

        yield items

        next_page = 'https://www.nepremicnine.net/oglasi-prodaja/slovenija/hisa/' + str(RealestateSpider.page_number) +  '/'
        #print(next_page)

        # if next_page is not None: # for buttons
        if RealestateSpider.page_number < 180:  # then only make sure to go to the next page
            # if yes then increase it --> for paginations
            time.sleep(1)
            RealestateSpider.page_number += 1
            # parse automatically checks for response.follow if its there when its done with this page
            # this is a recursive function
            # follow next page and where should it after following
            yield response.follow(next_page, self.parse)  # want it to go back to parse

你能告诉我我在这里用 css 选择器做错了什么吗?

【问题讨论】:

    标签: python web-scraping scrapy css-selectors


    【解决方案1】:

    您的选择器正在a.slika 内寻找a 元素。这应该可以解决您的问题:

    all_links = response.css('a.slika ::attr(href)').extract()
    

    这些将是相对 url,您可以使用 response.urljoin() 以使用您的响应 url 作为基域来构建绝对 url。

    【讨论】:

    • 太棒了,谢谢!!您能告诉我如何在现有代码中使用 urljoin() 吗?你需要一个 for 循环吗?
    • 是的,你需要一个循环。我建议作为列表理解:[response.urljoin(url) for url in all_links ]
    猜你喜欢
    • 2019-11-02
    • 2021-12-22
    • 2014-02-06
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多