【问题标题】:newbie in scrapy : how to response.css scrape the text part?scrapy中的新手:如何response.css刮掉文本部分?
【发布时间】:2014-09-12 20:51:20
【问题描述】:

当我练习时,我只想捕捉文本部分(1,2,3,4,5...),没有部分我怎么写 response.css("td[class='c1' ]")?

scrapy shell "https://tw.movies.yahoo.com/chart.html"
response.css("td[class='c1']")

【问题讨论】:

    标签: python css-selectors web-scraping scrapy


    【解决方案1】:

    这里有两种选择,一种使用css(),另一种使用xpath()

    >>> response.css("td.c1 > span::text").extract()
    [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'10', u'11', u'12', u'13', u'14', u'15', u'16', u'17', u'18', u'19', u'20']
    >>> response.xpath("//td[@class='c1']/span/text()").extract()
    [u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'10', u'11', u'12', u'13', u'14', u'15', u'16', u'17', u'18', u'19', u'20']
    

    【讨论】:

    • 哇,太神奇了!!但是有没有办法去掉'u'?
    • @user2492364 只是一个unicode literal,不用担心。
    • 你可以通过 .encode('utf8') 删除 u
    • 在您的代码 - response.css("td[class='c1']") 中,您可以在引号前添加 ::text:response.css("td[class='c1']::text")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多