- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在尝试使用 Scrapy(xpath) 从 Kbb 的 HTML 中的脚本标签中提取数据。但我的主要问题是识别正确的 div 和脚本标签。我刚开始使用 xpath,非常感谢任何帮助!
<script type="text/javascript" src="http://s1.kbb.com/combine/IncentivesPilotJs/949332058"></script>
<input type="hidden" id="ResaleValueUrl" value="/ymmt/resalevalue/?vehicleid=392396" />
<input type="hidden" id="Intent" value="buy-used" />
<!--[if lt IE 9]>
<script>
window.FlashCanvasOptions = {
swfPath: "/js/canvas/FlashCanvas/UCMarketMeter/"
};
</script>
<script type="text/javascript" src="http://s1.kbb.com/combine/YmmtMarketMeterFlashCanvasJs/795892638"></script>
<![endif]-->
<script type="text/javascript" src="http://s1.kbb.com/combine/YMMTOverview/1527402533"></script>
<script type="text/javascript" src="http://s1.kbb.com/combine/YmmtPricingOverviewBuyUsedJs/-1416499456"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
KBB.Vehicle.Pages.PricingOverview.Buyers.setup({
//Workaround until we get cross domain working for Flash
imageDir: window.FlashCanvasOptions ? "/Content/images" : "http://file.kelleybluebookimages.com/kbb/images/marketmeter",
vehicleId: "392396",
zipCode: "78701",
mileage: "10000",
intent: "buy-used",
priceType: "retail",
condition: "good",
options: "392396|53635|78701|100|10|",
price: "17074",
manufacturer: "Nissan",
model: "Altima",
year: "2014",
style: "2.5 S Sedan 4D",
category: "",
hasCpo: true,
meetsCpoReq: true,
showOthersPaid: false,
data: {
"values": {
"cpo": {
"priceMin": 17335.0,
"price": 18275.0,
"priceMax": 19214.0
},
"fpp": {
"priceMin": 15286.0,
"price": 17074.0,
"priceMax": 18861.0
},
"privatepartyexcellent": {
"priceMin": 0.0,
"price": 16064.0,
"priceMax": 0.0
},
"privatepartyfair": {
"priceMin": 0.0,
"price": 14081.0,
"priceMax": 0.0
},
"privatepartygood": {
"priceMin": 0.0,
"price": 15454.0,
"priceMax": 0.0
},
"privatepartyverygood": {
"priceMin": 0.0,
"price": 15715.0,
"priceMax": 0.0
},
"retail": {
"priceMin": 0.0,
"price": 17875.0,
"priceMax": 0.0
}
},
"timAmount": 0.0,
"monthlyPayments": {
"cpo": {
"vehiclePrice": 18275.0,
"rate": 2.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 348.0
},
"fpp": {
"vehiclePrice": 17074.0,
"rate": 4.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 342.0
},
"privatepartyexcellent": {
"vehiclePrice": 16064.0,
"rate": 4.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 322.0
},
"privatepartyfair": {
"vehiclePrice": 14081.0,
"rate": 4.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 282.0
},
"privatepartygood": {
"vehiclePrice": 15454.0,
"rate": 4.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 309.0
},
"privatepartyverygood": {
"vehiclePrice": 15715.0,
"rate": 4.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 315.0
},
"retail": {
"vehiclePrice": 17875.0,
"rate": 4.9,
"terms": 60.0,
"taxAndTitle": 6.5,
"downPay": 0.0,
"amount": 358.0
}
},
"scale": {
"scaleLow": 14081.0,
"scaleHigh": 19214.0
},
"transactions": {
"below": 7,
"between": 17,
"above": 3
}
},
adPriceRanges: {"AdPriceRange":[{"PriceMin":0,"PriceMax":8499,"AdPRValue":1},{"PriceMin":8500,"PriceMax":18499,"AdPRValue":2},{"PriceMin":18500,"PriceMax":23499,"AdPRValue":3},{"PriceMin":23500,"PriceMax":28499,"AdPRValue":4},{"PriceMin":28500,"PriceMax":33499,"AdPRValue":5},{"PriceMin":33500,"PriceMax":38499,"AdPRValue":6},{"PriceMin":38500,"PriceMax":43499,"AdPRValue":7},{"PriceMin":43500,"PriceMax":48499,"AdPRValue":8},{"PriceMin":48500,"PriceMax":53499,"AdPRValue":9},{"PriceMin":53500,"PriceMax":63499,"AdPRValue":10},{"PriceMin":63500,"PriceMax":73499,"AdPRValue":11},{"PriceMin":73500,"PriceMax":1000000,"AdPRValue":12}]}});
});
$('.foot-note').hide();
$(window).on('popstate', function() {
KBB.Vehicle.Pages.PricingOverview.Buyers.stateChangeHandler();
});
</script>
Scrapy Code:
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
import scrapy
from kbb.items import kbbItem
class kbbSpider(scrapy.Spider):
name = "kbb"
allowed_domains = ["kbb.com"]
start_urls = [
"http://www.kbb.com/nissan/altima/2014/25-s-sedan-4d/?vehicleid=392396&intent=buy-used&10000&good&pricetype=retail"
]
def parse(self, response):
sel=Selector(response)
#sites=sel.xpath('//div')
items=[]
#for site in sites:
item=kbbItem
item['priceMin']=site.xpath('//div/script').extract[35][915:922]
return items
我最后想从 fpp
填充 priceMin
、price
、priceMax
和从 retail 填充价格
字段到我的项目中。目前我正在使用索引来获取这些值,但想知道是否有更简单的方法。
最佳答案
问题是所需的数据在 Javascript 代码中。而且,您当前依赖行索引的方法非常脆弱且不可靠。
思路是定位包含所需数据的script
标签,使用regular expressions要获取包含价格的对象/字典,请借助 json
module 将对象加载到 Python 字典中并获得所需的信息。
来自 Scrapy Shell 的演示:
In [1]: import re
In [2]: import json
In [3]: pattern = re.compile(r"KBB\.Vehicle\.Pages\.PricingOverview\.Buyers\.setup\(.*?data: ({.*?}),\W+adPriceRanges", re.MULTILINE | re.DOTALL)
In [4]: data = response.xpath("//script[contains(., 'KBB.Vehicle.Pages.PricingOverview.Buyers.setup')]/text()").re(pattern)[0]
In [5]: data = data.replace("//Workaround until we get cross domain working for Flash", "")
In [6]: data_obj = json.loads(data)
In [7]: data_obj['values']['fpp']
Out[7]: {u'price': 15569.0, u'priceMax': 17356.0, u'priceMin': 13781.0}
In [8]: data_obj['values']['retail']
Out[8]: {u'price': 16370.0, u'priceMax': 0.0, u'priceMin': 0.0}
关于javascript - 使用 Scrapy 从 HTML 中的 <script> 标签中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503643/
前言: 一直是想知道一条SQL语句是怎么被执行的,它执行的顺序是怎样的,然后查看总结各方资料,就有了下面这一篇博文了。 本文将从MySQL总体架构--->查询执行流程--->语句执
哪个更好用或者更方便: ... 或 ... 最佳答案 你真的需要类型属性吗?如果您使用的是 HTML5,则不会。否则,是的。 HTML 4.01 和 XHTML 1.0 指定了 type属性是必需的,
哪个更好用或者更方便: ... 或 ... 最佳答案 你真的需要类型属性吗?如果您使用的是 HTML5,则不会。否则,是的。 HTML 4.01 和 XHTML 1.0 指定了 type属性是必需的,
我在 unix shell 脚本中遇到了一个问题。让我用一个非常简单的例子来问你。 假设,我正在获取用户输入并比较两个数字。 echo "Enter the first number" read a
是否可以在中制作文本?上标?如果是,怎么办? Meraki Baked Goods (Post 7: Dad's 50th) 最佳答案 以下似乎对我有用: Dad's 50ᵗʰ
我有一个转发器控件,其中 部分没有出现。 “Display”存储的代码设置如下: item.Display = " AudioPlayer.embed('ffcedea7-4822-465f-
在许多 .resx 文件中,我遇到了这样的事情: OurLabel 我的问题是:为什么属性name在许多情况下 - 但并非总是如此 - 从 >> 开始? 最佳答案 您在 Winfo
我正在尝试将代码添加到脚本中,但它不起作用 var script= document.createElement('script'); script.type= 'text/javas
我想编写正则表达式来读取 block 内的数据在 HTML 中。作为脚本,我认为空白具有灵 active 。为了使我的正则表达式模式健壮,我必须预测不同数量的空白。也许有比在我的模式中放置许多空白匹
基本上,我讨厌必须更改标题代码以及位于 中的页面标题。 header 中的标签....我可以使用 JavaScript 或 HTML 来自动设置某个 标题中的内容是否等于页面标题? 最佳答案 是的,
我可以使用以下代码提取所有标签。但是,我不知道如何在 之间查看内部。和标签。特别是,假设我只想要这一部分(中间还有更多内容,但我对此不感兴趣): var quoteData
有什么方法可以覆盖无法直接编辑的页面标题,只能在页眉中添加 Javascript? 我不能直接编辑的行是: Title of the page 我能想到的解决这个问题的唯一方法是在我可以通过我的门户后
我正在尝试在 fancybox 的内容方法中加载 javascript 内容。 $('#streaminput').on("click", function() { $('#streaminpu
我试图在 2 小时内解决这个问题,但运气不佳,也许这不是那么技术性,但我需要帮助! 我有一个 AJAX 脚本,需要将 GET 请求发送到同一页面上的 php 脚本。PHP 脚本这样终止 i
如何创建表单来删除标签并删除标签之间的内容或脚本。请任何人为我制作一份表格(我已经问过这个问题,但没有找到任何有用的答案。) 像这样的形式:(此形式仅删除标签,不删除text标签之间的脚本) //
我现在已经为此苦苦挣扎太久了。 我想做的只是简单地堆叠后续内容,就像您将一张 1 列 3 行的表格一样。我试图放弃旧的 HTML 和 CSS 方法,转而采用首选的 HTML5 页面结构方法。 像这样.
我正在尝试为 my chat box 创建通知,当有人向您发送新消息时,会在“谈话”标题旁边看到。我尝试过多种方法,但从来没有成功过。 a busy cat http://goawaymom.com/
我最初并不期望需要调用除 Iframe 之外的任何内容,但显然我错了。这是我的代码,您可以看到为什么它破坏了脚本中的脚本-/脚本有什么方法可以解决这个问题? $ad_blocks = arra
我希望能够在正在播放的视频之上使用全屏文本区域。我以为 position: fixed;顶部:0;可能有效,但没有。 HTML CSS * { box-sizing: border-box;
我找到了how to load external JS file into moodle?但这并不能解决我的问题。 我必须加载一个外部 JS 文件,因为它是 VOD 服务的播放器。 必须在 中调用它
我是一名优秀的程序员,十分优秀!