Scrapy - 编码问题 - 删除引号

时间:2016-09-30 01:14:47

标签: python scrapy

我有这堂课:

class PitchforkTracks(scrapy.Spider):
    name = "pitchfork_tracks"
    allowed_domains = ["pitchfork.com"]
    start_urls = [
                    "http://pitchfork.com/reviews/best/tracks/?page=1",
                    "http://pitchfork.com/reviews/best/tracks/?page=2",
                    "http://pitchfork.com/reviews/best/tracks/?page=3",
                    "http://pitchfork.com/reviews/best/tracks/?page=4",
                    "http://pitchfork.com/reviews/best/tracks/?page=5",
    ]
    def parse(self, response):

        for sel in response.xpath('//div[@class="track-details"]/div[@class="row"]'):
            item = PitchforkItem()
            item['artist'] = sel.xpath('.//li/text()').extract_first()  
            item['track'] = sel.xpath('.//h2[@class="title"]/text()').extract_first()  
            yield item

抓取这个项目:

<h2 class="title" data-reactid="...>“Colours”</h2>
然而,

结果印刷如下:

{'artist': u'The Avalanches', 'track': u'\u201cColours\u201d'}

我在哪里以及如何删除quotes,即\u201c\u201d

1 个答案:

答案 0 :(得分:2)

parse(self, response)内添加:

item['track'] = sel.xpath('.//h2[@class="title"]/text()').extract_first().strip(u'\u201c\u201d')