Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从刮痕上刮下产品,然后分页_Python_Xpath_Scrapy - Fatal编程技术网

Python 从刮痕上刮下产品,然后分页

Python 从刮痕上刮下产品,然后分页,python,xpath,scrapy,Python,Xpath,Scrapy,我正在尝试使用scrapy从中提取数据。您可以查看或查看页面 我想从页面上抓取的数据是产品名称、价格、最小订单、公司名称、图像的Url 这张图片显示了我想要刮的东西 我的Python代码 问题 这段代码从页面的36个项目中只删除了21个 如何遵循分页链接 你能帮忙吗 修改代码,以便从页面中删除所有数据 修改代码以遵循分页并保持刮取 尝试在每个XPath的开头加上点,例如//h2/a/@title。它将指向上下文(products)@Andersson,这似乎是可行的,但它只删除了一半的数

我正在尝试使用scrapy从中提取数据。您可以查看或查看页面

我想从页面上抓取的数据是产品名称、价格、最小订单、公司名称、图像的Url

这张图片显示了我想要刮的东西

我的Python代码 问题
  • 这段代码从页面的36个项目中只删除了21个
  • 如何遵循分页链接
你能帮忙吗
  • 修改代码,以便从页面中删除所有数据
  • 修改代码以遵循分页并保持刮取

尝试在每个XPath的开头加上点,例如
//h2/a/@title
。它将指向上下文(
products
)@Andersson,这似乎是可行的,但它只删除了一半的数据(36个数据中只有21个),那么分页呢?使用一个(外部)的
for
循环来迭代页面。试试URL
“https://www.alibaba.com/catalog/agricultural-growing-media_cid144?spm=a2700.9161164.1.2.4a934e02VlSXiW&page=%s%i
其中
i
是页码(从
1开始)
# -*- 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?spm=a2700.9161164.1.2.4a934e02VlSXiW']

def parse(self, response):
    for products in response.xpath('.//div[contains(@class, "m-gallery-product-item-wrap")]/div/div'):
        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(),
            #'image_url': products.xpath('.//div[@class=""]/').extract_first(),
         }
        yield item