【问题标题】:xpath finds element in developers console but not in scrapy.responsexpath 在开发人员控制台中找到元素,但在 scrapy.response 中没有
【发布时间】:2015-09-22 22:05:52
【问题描述】:

我正在尝试使用此 xpath 从第一张票 here 页面中获取价格:

'.//*[@class="price"]/text()'

这在开发人员的控制台中有效,但在我使用 response.xpath 在 scrapy shell 中运行时无效。我也尝试在 shell 中关注:

'.//*[@class="initial"]/div[@class="price"]/text()'

'//*[@id="tVB901769989"]/div[1]/div[4]'(虽然我不认为id属性可以这样在shell中使用)。

我使用的 xpath 是否有问题,或者页面的工作方式有什么不同?任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: html python-2.7 xpath web-scraping scrapy


    【解决方案1】:

    发生这种情况是因为您正在检查不同的请求,您看到的页面在该文件中没有您需要的信息,但它是动态获取的,在这种情况下来自:www.vividseats.com/javascript/tickets。 shtml?productionId=1771684

    在那里你可以查看json格式的价格,我认为这是一个项目:

    {
       "s":"Section 111",
       "r":"8-22",
       "q":"4",
       "p":"692.00",
       "i":"VB782041491",
       "d":"111",
       "n":"Zone Seating. The seller is committing to procure these tickets for you upon receipt of your order. After you place your order and your order is confirmed, we guarantee that your tickets will be within the listed zone
     or section listed or one comparable and that you will receive these tickets in time for the event or
     your money back. Orders exceeding four tickets may be split up into different rows within the requested
     zone or section.",
       "f":"0",
       "l":"Section 111",
       "g":"0",
       "e":"0",
       "h":"07/21/15",
       "t":"0",
       "v":"",
       "c":"84352",
       "z":"1",
       "rhdn":"0",
       "ind":"0",
       "sd":"0"
    }
    

    其中p 包含价格。

    【讨论】:

    • 感谢您的回答!我现在只有两个问题。首先是您如何获得票证的 json 数据的链接。似乎必须从请求或其他内容中检索该文件。此外,由于事实证明必须从 JSON 数据中检索价格,有没有什么办法可以在循环内完成?顺便说一句,我正在使用 scrapy 从上一页获取数据(使用活动和所有相应票务页面的链接)以及每个票务页面的最低价格。
    • 你需要检查访问页面的所有请求,我个人使用firebug(在firefox上)。关于“循环”,我真的不明白你的意思,但是可以将 json 解析为字典,然后你可以获取信息(你可以用正则表达式以讨厌的方式获取它),但是有很多方法。
    • 我能够找到每张票使用的 json 文件,但现在我需要在页面上找到第一个价格(用“p”表示),然后将其存储到 Scrapy 项目加载器中。你知道最好的方法吗?在这种情况下最好使用一些 jquery 函数还是应该使用正则表达式?
    • 使用json 模块?
    猜你喜欢
    • 2019-07-31
    • 2023-01-21
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多