【问题标题】:How to use the css selector to extract urls with python's scrapy?如何使用css选择器通过python的scrapy提取url?
【发布时间】:2017-04-24 16:03:03
【问题描述】:

为了学习scrapy我正在爬取本网站的所有元素:http://quotes.toscrape.com/random

但是,我不明白如何抓取作者 url bio。我尝试使用 css 选择器:

>>> response.css('a::attr(href)').extract()
['/', '/login', '/author/Ralph-Waldo-Emerson', '/tag/life/page/1/', '/tag/regrets/page/1/', 'https://www.goodreads.com/quotes', 'https://scrapinghub.com']

然后:

>>> response.css('small.quote>span>a::attr(href)').extract()

不过,我没有得到作者的简历网址。因此,如何使用 css 选择器获取上述 url?。

更新

我已经知道我可以做到:

response.css('a::attr(href)').extract()[2]

但是,我想这并不可靠。知道如何获取生物链接吗?

【问题讨论】:

    标签: python css python-3.x scrapy web-crawler


    【解决方案1】:

    这可能有效:

    >>> os.path.dirname(response.url)
    'http://quotes.toscrape.com'
    
    >> response.css('a::attr(href)').extract()[2]
    u'/author/Bob-Marley'
    
    >>> os.path.dirname(response.url) + response.css('a::attr(href)').extract()[2]
    u'http://quotes.toscrape.com/author/Bob-Marley'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2014-02-06
      相关资源
      最近更新 更多