我正试着从this page刮。我写的代码从36条中抽出10条我找不到问题。在
如果我在shell上单独运行查询,它似乎会从页面中提取所有数据项,但是当我运行spider时,并不是所有的数据都提取到CSV文件中。有什么问题吗?在
# -*- coding: utf-8 -*-
import scrapy
class AlibabaSpider(scrapy.Spider):
name = 'alibaba'
allowed_domains = ['alibaba.com']
start_urls = ['https://www.alibaba.com/catalog/agricultural-growing-media_cid144?page=1']
def parse(self, response):
for products in response.xpath('//div[contains(@class, "m-gallery-product-item-wrap")]'):
item = {
'product_name': products.xpath('.//h2/a/@title').extract_first(),
'price': products.xpath('.//div[@class="price"]/b/text()').extract_first().strip(),
'min_order': products.xpath('.//div[@class="min-order"]/b/text()').extract_first(),
'company_name': products.xpath('.//div[@class="stitle util-ellipsis"]/a/@title').extract_first(),
'prod_detail_link': products.xpath('.//div[@class="item-img-inner"]/a/@href').extract_first(),
'response_rate': products.xpath('.//i[@class="ui2-icon ui2-icon-skip"]/text()').extract_first().strip(),
#'image_url': products.xpath('.//div[@class=""]/').extract_first(),
}
yield item
蜘蛛网结果仅刮出10个物品
我们可以看到所有36个产品的标题都已提取
如果您查看spider的日志输出,问题就显而易见了。在
如您所见,异常(
AttributeError
)正在被引发,因为并非所有的项目都有价格和/或响应率。在解决此问题的一种可能方法是在提取时使用不同的默认值:
^{pr2}$现在下面的代码似乎运行得非常好。在
相关问题 更多 >
编程相关推荐