我正在使用Scrapy 0.24(使用Pythong 2.7.9,在Windows 7 64位上)搜索数据的页面正在返回–
而不是您的短划线数字实体代码–
查看页面源时可以看到。这只是一个例子当然,这适用于页面上存在的所有特殊字符。我正在抓取的页面<meta charset="utf-8">
中声明了<head>
。
为了成功将正确的字符写入.csv文件,我需要在Scrapy代码中做些什么?
经过一些新的研究后,我discovered this answer提出另一个问题。显然,Python 2.7中的csv模块不支持Unicode。虽然我不知道如何使用这些信息来解决我的问题。
这是我的蜘蛛代码。
products.py
import scrapy
import urlparse
from allenheath.items import ProductItem
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
class productsSpider(scrapy.Spider):
name = "products"
allowed_domains = ["http://www.allen-heath.com/"]
start_urls = [
"http://www.allen-heath.com/ahproducts/ilive-80/",
"http://www.allen-heath.com/ahproducts/ilive-112/"
]
def parse(self, response):
for sel in response.xpath('/html'):
item = ProductItem()
item['model'] = sel.css('#prodsingleouter > div > div > h2::text').extract() # The value I'd like to use to name my images.
item['shortdesc'] = sel.css('#prodsingleouter > div > div > h3::text').extract()
item['desc'] = sel.css('#tab1 #productcontent').extract()
item['series'] = sel.css('#pagestrip > div > div > a:nth-child(3)::text').extract()
item['imageorig'] = sel.css('#prodsingleouter > div > div > h2::text').extract()
item['image_urls'] = sel.css('#tab1 #productcontent .col-sm-9 img').xpath('./@src').extract()
item['image_urls'] = [urlparse.urljoin(response.url, url) for url in item['image_urls']]
yield item
我使用命令提示符中的以下命令运行蜘蛛:
scrapy crawl products -o items.csv