【问题标题】:I want to fetch the data in data.json using this query or code我想使用此查询或代码获取 data.json 中的数据
【发布时间】:2019-11-18 18:01:55
【问题描述】:

我正在使用 VS 代码 + git bash 将这些数据抓取到 JSON 中。但是我没有将任何数据输入 JSON,或者我没有在 JSON 中获得任何数据。 JSON 文件为空。

import scrapy

class ContactsSpider(scrapy.Spider):
    name= 'contacts'

    start_urls = [
        'https://app.cartinsight.io/sellers/all/amazon/'
    ]

    def parse(self, response):
        for contacts in response.xpath("//td[@title= 'Show Contact']"):
            yield{
                'show_contacts_td': contacts.xpath(".//td[@id='show_contacts_td']").extract_first()
                }
            next_page= response.xpath("//li[@class = 'stores-desc hidden-xs']").extract_first()
            if next_page is not None:
                next_page_link= response.urljoin(next_page)
                yield scrapy.Request(url=next_page_link, callback=self.parse)

【问题讨论】:

  • 你尝试过python的json模块吗?我想这对你会很有帮助。
  • 我会尝试让你知道如果不起作用!谢谢你!
  • 尝试阅读我的这个答案link 这将帮助您读取和写入数据到 json 文件

标签: web-scraping scrapy


【解决方案1】:

您要抓取的 URL https://app.cartinsight.io/sellers/all/amazon/ 正在重定向到此 URL https://app.cartinsight.io/。第二个 URL 不包含此 XPath "//td[@title= 'Show Contact']",这会导致在 parse 方法中跳过 for 循环,因此您不会得到想要的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多