【问题标题】:How to convert script path into JSON?如何将脚本路径转换为 ​​JSON?
【发布时间】:2020-08-12 07:15:44
【问题描述】:

我正在抓取这个网站:https://www.epicery.com/c/promos?gclid=CjwKCAjw97P5BRBQEiwAGflV6bGzNEAz7MTIrgelBkTR277v3lhStP5tH0wgxuLj1ytlcQAAjb-cxBoCsVwQAvD_BwE 我正在尝试检索脚本路径中的一些信息,例如描述。 我使用 xpath 获取脚本内容并制作一些正则表达式并尝试将其加载为 json:

script_path = response.xpath('/html/body/script[1]').get()
j_list = re.findall(r'\[(.*)\}\]',script_path)
j = j[0].replace("'","")
json_script = json.loads(j)

但是我有以下我无法处理的错误:

 raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 152446 (char 152445)

【问题讨论】:

  • 你到底想废弃什么?
  • @Vin 我正在尝试在 scipt 标签中获取一些内容,例如 description 或 gps_lat long

标签: python-3.x xpath web-scraping scrapy


【解决方案1】:

我不确定你想要什么,但这对我有用:

def parse(self, response):
    taxons_str = response.xpath('//script[contains(., "var taxons")]/text()').re_first(r'(?s)var taxons = (.+?)var shops')
    if taxons_str:
        taxons = json.loads(taxons_str)
        for product in taxons:
            process_your_product(product)

【讨论】:

    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多