无法从pag中提取所有数据

2024-05-02 04:10:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着从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

我的结果图像

enter image description here

蜘蛛网结果仅刮出10个物品

enter image description here

我们可以看到所有36个产品的标题都已提取


Tags: 代码textnamedivcomresponsepageextract
2条回答

如果您查看spider的日志输出,问题就显而易见了。在

Traceback (most recent call last):
  File "c:\program files\python37\lib\site-packages\scrapy\utils\defer.py", line 102, in iter_errback
    yield next(it)
  File "c:\program files\python37\lib\site-packages\scrapy\spidermiddlewares\offsite.py", line 30, in process_spider_output
    for x in result:
  File "c:\program files\python37\lib\site-packages\scrapy\spidermiddlewares\referer.py", line 339, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "c:\program files\python37\lib\site-packages\scrapy\spidermiddlewares\urllength.py", line 37, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "c:\program files\python37\lib\site-packages\scrapy\spidermiddlewares\depth.py", line 58, in <genexpr>
    return (r for r in result or () if _filter(r))
  File "D:\Users\Ivan\Documents\Python\a.py", line 18, in parse
    'response_rate': products.xpath('.//i[@class="ui2-icon ui2-icon-skip"]/text()').extract_first().strip(),
AttributeError: 'NoneType' object has no attribute 'strip'
2018-09-08 19:40:36 [scrapy.core.engine] INFO: Closing spider (finished)
2018-09-08 19:40:36 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 262,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 50753,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2018, 9, 8, 17, 40, 36, 231107),
 'item_scraped_count': 9,
 'log_count/DEBUG': 11,
 'log_count/ERROR': 1,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/AttributeError': 1,
 'start_time': datetime.datetime(2018, 9, 8, 17, 40, 33, 668475)}

如您所见,异常(AttributeError)正在被引发,因为并非所有的项目都有价格和/或响应率。在

解决此问题的一种可能方法是在提取时使用不同的默认值:

^{pr2}$

现在下面的代码似乎运行得非常好。在

# -*- 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

相关问题 更多 >