gpt4 book ai didi

python - xpath 标签不确定要放什么

转载 作者:行者123 更新时间:2023-11-28 22:39:31 25 4
gpt4 key购买 nike

我有一个页面,我正试图从中降低价格。这是该行的一瞥。

<div itemprop="price" class="js-price-display Price Price--flair Price--medium hide-content-m price-display"> <span class="Price-sup">$</span>34<span class="Price-mark">.</span>96 </div>']

我尝试了很多不同的方法来获得它,但总是不够用。我得到的最好的是这 2 个。(顺便说一句,价格是 34.96 美元)

selector.xpath('//div[@itemprop="price"]/text()').extract()

这给了我

[u' ', u'34', u' ', u' ', u'34', u'96 ']

这样走

selector.xpath('//div[@class="js-price-display Price Price--flair Price--medium hide-content-m price-display"]').extract()

这是给了我这个结果

[u'<div itemprop="price" class="js-price-display Price Price--flair Price--medium hide-content-m price-display"> <span class="Price-sup">$</span>34<span class="Price-mark">.</span>96 </div>']

如果我只得到跨度,它会给我 $ 和 .但我不知道那个属性(或属性或值或其他)在实际数量的末尾的跨度“之后”被称为什么。我很想得到第一部分、时期和第二部分,但我会接受比我得到的更好的东西。最后,我运行了这段测试代码来查看我放入其中的网页 block 的属性。这是我跑的

for item in selector.xpath('.//*[@itemprop]'):
print "Item:", item.xpath('@itemtype').extract()
for property in item.xpath('.//*[@itemprop]'):
print "Property:",
print property.xpath('@itemprop').extract(),
print property.xpath('string(.)').extract()
for position, attribute in enumerate(property.xpath('@*'), start=1):
print "attribute: name=%s; value=%s" % (
property.xpath('name(@*[%d])' % position).extract(),
attribute.extract())
print
print

我得到了这些结果。

C:\Python27\bff\bff\spiders>python test.py
Item: [u'http://schema.org/Offer']
Property: [u'priceCurrency'] [u'']
attribute: name=[u'itemprop']; value=priceCurrency
attribute: name=[u'content']; value=USD

Property: [u'price'] [u' $34.96 ']
attribute: name=[u'itemprop']; value=price
attribute: name=[u'class']; value=js-price-display Price Price--stylized Price--large hide-content display-inline-m price-display

Property: [u'price'] [u' $34.96 ']
attribute: name=[u'itemprop']; value=price
attribute: name=[u'class']; value=js-price-display Price Price--flair Price--medium hide-content-m price-display

Property: [u'availability'] [u'']
attribute: name=[u'itemprop']; value=availability
attribute: name=[u'itemtype']; value=http://schema.org/ItemAvailability
attribute: name=[u'content']; value=InStock

它怎么像 BOOM,就像我需要我的东西来展示但我无法得到它一样?我尝试同时使用这些类和 itemprop 值,并不断获得与我发布的内容类似的内容。预先感谢您的帮助。

最佳答案

通过使用 /text(),您将仅获得作为当前上下文元素的直接子节点的文本节点。通过使用 //text(),您将获得当前上下文元素中的所有文本节点,无论是直接子元素还是嵌套元素,但分隔的文本将作为单独的文本节点返回。

您需要的是定位div 元素,然后在每个div 上调用XPath string() 函数,这就是您的测试代码do 输出期​​望值。如果一次只有一个 div,那么下面的 XPath 也可以工作:

selector.xpath('string(//div[@itemprop="price"])').extract()

否则,您需要遍历 div 元素,然后在 for 循环体中调用 string()

关于python - xpath 标签不确定要放什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com