这是我的代码:
# -*- coding: utf-8 -*-
import scrapy
from ..items import LowesspiderItem
from scrapy.http import Request
import requests
import pandas as pd
class LowesSpider(scrapy.Spider):
name = 'lowes'
def start_requests(self):
start_urls = ['https://www.lowes.com/pd/ZLINE-KITCHEN-BATH-Alpine-Brushed-Nickel-2-Handle-Widespread-Bathroom-Sink-Faucet-with-Drain/1002623090']
for url in start_urls:
yield Request(url,
headers={'Cookie': 'sn=2333;'}, #Preset a location
meta={'dont_merge_cookies': True, #Allows location cookie to get through
'url':url}) #Using to get the product SKU
def parse(self, response):
item = LowesspiderItem()
#get product price
productPrice = response.css('.sc-kjoXOD iNCICL::text').get()
item["productPrice"] = productPrice
yield item
所以这在上周有效,但是后来我的蜘蛛被弃用了,因为我认为网站被修改了,所以我所有的选择器都坏了。我正在尝试寻找价格的新选择器,但我没有任何运气。
首先,我检查了此数据是否是动态创建的(不是),所以我认为使用普通的scrapy应该没问题,如果我错了,请更正我。这是禁用JavaScript时页面的屏幕截图
然后,我检查了页面源代码,然后按CTRL + F的价格找到了我想要/需要的选择器。
这是文本截图(如果有帮助的话)
left"><svg data-test="arrow-left" color="interactive" viewBox="0 0 24 24" class="sc-jhAzac boeLhr"><path d="M16.88 5.88L15 4l-8 8 8 8 1.88-1.88L10.773 12z"></path></svg></button><button class="arrowNav right"><svg data-test="arrow-right" color="interactive" viewBox="0 0 24 24" class="sc-jhAzac boeLhr"><path d="M8.88 4L7 5.88 13.107 12 7 18.12 8.88 20l8-8z"></path></svg></button></div></div></div></div></div><div class="sc-iQKALj jeIzsl"><div class="sc-gwVKww kbEspX"><div class="sc-esOvli jhvGZy"><div tabindex="0" class="styles__PriceWrapper-sc-1ezid1y-0 cgqauT"><span class="finalPrice"><div class="sc-kjoXOD iNCICL">$314.96 </div><span class="aPrice large" aria-hidden="true"><sup itemProp="PriceCurrency" content="USD" aria-hidden="true">$</sup><span aria-hidden="true">314</span><sup aria-hidden="true">.<!-- --
,这是页面源的链接: 查看来源:https://www.lowes.com/pd/ZLINE-KITCHEN-BATH-Alpine-Brushed-Nickel-2-Handle-Widespread-Bathroom-Sink-Faucet-with-Drain/1002623090
编辑*
浏览网站,我认为此选择器会更有意义:
productPrice = response.css('.primary-font jumbo strong art-pd-contractPricing::text').get()
价格嵌套在此选择器下,但我仍然一无所获。我原本以为是因为这是“销售”价格,所以我检查了它是否是通过JavaScript生成的,不是。
答案 0 :(得分:2)
正如最粗略地使用scrapy shell会向您显示的那样,response.css('.sc-kjoXOD iNCICL')
不是适合您的情况的正确CSS选择器,因为space means descendant
根据您对与其他页面不同的定价页面的最新评论,需要使用更通用的选择器。值得庆幸的是,Lowe似乎采用了https://schema.org/Offer标准,该标准定义了price itemprop,这意味着您对标记不会从销售页面更改为非销售页面的信心高于平均水平
for offer in response.css('[itemtype="http://schema.org/Offer"]'):
offered_price = offer.css('[itemprop="price"][content]').xpath('@content').get()
该评论的星号是schema.org标准允许以多种方式对itemprop
信息进行编码,而它们对content=""
属性的使用只是 current 方式,所以要注意这种变化
答案 1 :(得分:1)
So then, I inspected page source and just CTRL + F the price to find the selector that I'd want/need.
这不是原始来源-这是由于javascript而已更改的html代码。
您需要查看原始 html代码,因为scrapy可与原始html一起使用
您可以通过按CTRU + U或鼠标右键->查看页面源(不检查)
因此,您可以看到原始html代码和javascript更改的html代码之间存在显着差异。
原始代码的价格在多个地方出现。
在script
标记内。 -some options。
在input
标签内:
price = response.css("input[data-productprice]::attr(data-productprice)").extract_first()
在span
标记内:
price = response.css('span[itemprop="price"]::attr(content)').extract_first()
UPD fullprices和saleprices选择器将略有不同。
saleprice = response.css('span[itemprop="price"]::attr(content)').extract_first()
wasprice_text = response.css('span.art-pd-wasPriceLbl::text).extract_first()
if "$" in wasprice_text:
fullprice = wasprice_text.split("$")[-1]