python - Scrapy - 获取 Javascript 变量的值

标签 python python-2.7 scrapy

使用 Scrapy,如何获取 Javascript 变量的值....

这是我的代码...

<script rel="bmc-data">
      var match = 'yes';
      var country = 'uk';
      var tmData = {
        "googleExperimentVariation": "1",
        "pageTitle": "Child Care",
        "page_type": "claimed",
        "company_state": "wyostate",
        "company_city": "mycity"
                   };
</script>

我想检查 page_type 变量的值。如果其“声明”处理该页面,否则继续......

我已经看到了thisthis

我试过这个...

pattern = r'page_type = "(\w+)",'
response.xpath('//script[@rel="bmc-data"]').re(pattern)

当然这不起作用,因为我认为我的正则表达式是错误的。

最佳答案

您的正则表达式模式在这里有问题:

# you are looking for this bit: "page_type": "claimed",
re.findall('page_type": "(.+)"', html_body)
# ["claimed"]

或者在您的情况下,在 scrapy 选择器的上下文中:

response.xpath('//script[@rel="bmc-data"]').re('page_type": "(.+)"')

如果您需要像这样解析多个变量,我推荐 Paul 提到的答案,因为正则表达式并不总是像 xml 解析一样可靠。

关于python - Scrapy - 获取 Javascript 变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41718257/

相关文章:

python - 使用scrapy导出多种格式的抓取数据

python - 为什么这个 x 路径只带来一个值?

python - Django-registration 重新发送带有新代码的激活邮件

python - 如何在 sympy 中插入替换

python - 数据解析,Pythonic 方式

python-2.7 - Tkinter 中的多个窗口 (Python 2.7)

python - CMake 中的错误 Python 2 库

php - 使用 Python 连接器在 Mac OSx 上安装 Mysql

python - 如何在python中等待用户输入5秒

python - 属性错误: 'module' object has no attribute 'DATABASE' when using scrapy shell