【问题标题】:How to get href value from a link using its class name with CSS selector in scrapy?如何在scrapy中使用类名和CSS选择器从链接中获取href值?
【发布时间】:2021-06-05 08:21:19
【问题描述】:

<a class="a-link-normal a-text-normal" href="/Art-Dutch-Republic-1585-Everyman/dp/0297833693/ref=sr_1_1?keywords=9780297833697&amp;qid=1574351815&amp;sr=8-1"> <span class="a-size-medium a-color-base a-text-normal">Art of the Dutch Republic 1585 - 1718 (Everyman Art Library)</span> </a>

如何使用 CSS 选择器或 Xpath 获取 href 的值?

【问题讨论】:

  • 试试这个 xpath - //a[@class='a-link-normal a-text-normal']/@href
  • 如果你想要没有href属性的值,那么使用:string(//a[@class='a-link-normal a-text-normal']/@href)

标签: web-scraping scrapy


【解决方案1】:

这是一个例子:

    def parse(self, response):
        # iterate over all href
        for href in response.xpath("//a[@class='class-name']/@href"):
            # extract href as a string
            url = href.extract()

【讨论】:

    【解决方案2】:

    CSS 选择器示例:

    links = response.css("a.a-link-normal.a-text-normal::attr(href)").extract()
    

    【讨论】:

      【解决方案3】:

      试试这个response.css('.a-link-normal ::attr(href)').extract()

      【讨论】:

        【解决方案4】:

        您可以通过以下选择器来实现此目的

        a.your_calss_name::attr(href)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-22
          • 2014-02-06
          • 1970-01-01
          • 2015-10-27
          • 1970-01-01
          • 2019-01-18
          • 2016-08-24
          相关资源
          最近更新 更多