Character encoding 当使用Scrapy刮取网站时,如何确保所有字符都正确刮取?
我正在使用Scrapy刮取一个网站,但有些字符(如撇号)刮取不正确,也不总是相同的错误字符,即,我的结果集中有一个撇号显示为多个奇数字符。我如何确保所有字符都能正确刮取 编辑 我正试图用以下刮刀刮:Character encoding 当使用Scrapy刮取网站时,如何确保所有字符都正确刮取?,character-encoding,web-scraping,character,scrapy,Character Encoding,Web Scraping,Character,Scrapy,我正在使用Scrapy刮取一个网站,但有些字符(如撇号)刮取不正确,也不总是相同的错误字符,即,我的结果集中有一个撇号显示为多个奇数字符。我如何确保所有字符都能正确刮取 编辑 我正试图用以下刮刀刮: import urlparse import time from scrapy.http import Request from scrapy.spider import BaseSpider from scrapy.selector import Selector #from NT.items i
import urlparse
import time
from scrapy.http import Request
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
#from NT.items import NowTorontoItem
from scrapy.item import Item, Field
class NowTorontoItem(Item):
eventArtist = Field()
eventTitle = Field()
eventHolder = Field()
eventDetails = Field()
#venueName = Field()
eventAddress = Field()
eventLocality = Field()
eventPostalCode = Field()
eventPhone = Field()
eventURL = Field()
eventPrice = Field()
eventDate = Field()
internalURL = Field()
class MySpider(BaseSpider):
name = "NTSpider"
allowed_domains = ["nowtoronto.com"]
start_urls = ["http://www.nowtoronto.com/music/listings/"]
def parse(self, response):
selector = Selector(response)
listings = selector.css("div.listing-item0, div.listing-item1")
for listing in listings:
item = NowTorontoItem()
for body in listing.css('span.listing-body > div.List-Body'):
item ["eventArtist"] = body.css("span.List-Name::text").extract()
item ["eventTitle"] = body.css("span.List-Body-Emphasis::text").extract()
item ["eventHolder"] = body.css("span.List-Body-Strong::text").extract()
item ["eventDetails"] = body.css("::text").extract()
#item ["internalURL"] = body.css("a::attr(href)").extract()
time.sleep(1)
for body in listing.css('div.listing-readmore'):
item ["internalURL"] = body.css("a::attr(href)").extract()
# yield a Request()
# so that scrapy enqueues a new page to fetch
detail_url = listing.css("div.listing-readmore > a::attr(href)")
if detail_url:
yield Request(urlparse.urljoin(response.url,
detail_url.extract()[0]),
meta={'item': item},
callback=self.parse_details)
else:
yield item
def parse_details(self, response):
self.log("parse_details: %r" % response.url)
selector = Selector(response)
listings = selector.css("div.whenwhereContent")
for listing in listings:
for body in listing.css('tr:nth-child(1) td.small-txt.dkgrey-txt.rightInfoTD'):
item = response.meta['item']
#item ["eventLocation"] = body.css("span[property='v:location']::text").extract()
#item ["eventOrganization"] = body.css("span[property='v:organization'] span[property='v:name']::text").extract()
#item ["venueName"] = body.css("span[property='v:name']::text").extract()
item ["eventAddress"] = body.css("span[property='v:street-address']::text").extract()
item ["eventLocality"] = body.css("span[property='v:locality']::text").extract()
item ["eventPostalCode"] = body.css("span[property='v:postal-code']::text").extract()
item ["eventPhone"] = body.css("span[property='v:tel']::text").extract()
item ["eventURL"] = body.css("span[property='v:url']::text").extract()
item ["eventPrice"] = listing.css('tr:nth-child(2) td.small-txt.dkgrey-txt.rightInfoTD::text').extract()
item ["eventDate"] = listing.css('span[content*="201"]::attr(content)').extract()
yield item
我在一些结果中得到了字符,比如ée
和ée
。这些应该是像埃和ç这样的人物
编辑2
我不确定这个问题仅仅与我正在使用的文件查看器有关。当我在文本编辑器中打开第一个scrape时,一个撇号的格式为€™ 而在我的第二次尝试中,相同的撇号(来自相同的文本字符串)格式为-È。结果表明,数据实际上很好,但当我在Excel中打开并保存文件时,编码被破坏。我已经切换到,它特别要求在打开文档时对文档进行编码,并且一切正常 您能更具体一点并举例说明问题吗?@alecxe Done anddone@zgall1,您可能正在导出到CSV,并且输出文件的默认编码是UTF-8。您得到的数据可能很好,但是您需要告诉您的CSV查看器该文件是UTF8格式的。你在使用什么工具或程序?你是对的,我正在导出到CSV。我正在使用Excel查看数据。但是,如果我对同一数据进行两次不同的刮取,则不正确的字符不会一致地显示为同一组字符串。如果这只是用来查看数据的工具的问题,那怎么可能呢?@pault。请参阅我的第二次编辑以获取示例。