使用 Scrapy 抓取游戏商店时遇到问题 - HTML 如果有折扣和处理 null 则更改
Trouble crawling game stores using Scrapy - HTML changes if there is discount & dealing with null
我正在尝试使用 Scrapy 来抓取一系列游戏商店,并且都遇到了同样的问题。我正在使用 XPath,游戏价格的 HTML 会根据价格是简单地标记为 £ 20.09
还是 £ 20.09
加上一条线然后 £ 14.49
来更改显示折扣。
我很高兴有两列,was 20.09
(其中包含空值)和 now 14.49
之后的一列,但我无法弄清楚如何使用空值而不是仅仅取代以下所有内容一个。
这是我的网站 cdkeys 的代码 - https://www.cdkeys.com/pc/games?limit=50 有打折和没有打折的游戏。
allowed_urls = ['https://www.cdkeys.com/pc/games?limit=50?']
start_urls = ['https://www.cdkeys.com/pc/games/{pageno}?limit=50'.format(pageno=pageno)
for pageno in range(1, 10)]
def parse(self, response):
Games = response.xpath('//*[@id="root-wrapper"]/div/div[1]/div[2]/div[3]/div[2]/div[2]/ul/li/h2/a/text()').extract()
Prices = response.xpath('//span[starts-with(@id, "product-price-")]/span[1]/span/text()').extract()
for i, (Game, Price) in enumerate(zip(Games, Prices)):
yield {'index': i, 'Game': Game, 'Price':Price}
问题出在价格的 XPath 中,我可以得到只有折扣价的列表,或者只有没有折扣的游戏的价格列表,因为 HTML 对于这些类别来说是完全不同的。
阻止我简单地创建两个列表的原因是因为我使用的是 zip
和 enumerate
它只是迭代第一个 x
数量的游戏直到用完价格,而不是将每个游戏链接到相应的价格。
任何有关在 Prices
中仅显示正确价格或找到一种使用空值而不是替换以下值的方法的帮助将不胜感激。我对 python 和网络爬虫都是新手,只是想了解这一切。
我会采取不同的做法 - 逐一遍历产品项目,然后找到游戏名称、正常价格和折扣价:
def parse(self, response):
for game in response.css("ul.products-grid li.item"):
name = game.css("h2.product-name > a::text").extract_first()
old_price = game.css(".regular-price .price::text,.old-price .price::text").extract_first()
discount_price = game.css(".special-price .price::text").extract_first()
yield {
"name": name,
"old_price": old_price,
"discount_price": discount_price
}
对于第一页,您将获得以下输出:
{'old_price': u'$ 13.59', 'name': u'Stellaris: Utopia PC DLC', 'discount_price': None}
{'old_price': u' $ 9.49 ', 'name': u'Insurgency PC', 'discount_price': u' $ 1.99 '}
...
{'old_price': u' $ 81.59 ', 'name': u'Call of Duty Black Ops II 2 Digital Deluxe Edition PC ', 'discount_price': u' $ 13.59 '}
注意旧价格是如何填写有和没有折扣的。
我正在尝试使用 Scrapy 来抓取一系列游戏商店,并且都遇到了同样的问题。我正在使用 XPath,游戏价格的 HTML 会根据价格是简单地标记为 £ 20.09
还是 £ 20.09
加上一条线然后 £ 14.49
来更改显示折扣。
我很高兴有两列,was 20.09
(其中包含空值)和 now 14.49
之后的一列,但我无法弄清楚如何使用空值而不是仅仅取代以下所有内容一个。
这是我的网站 cdkeys 的代码 - https://www.cdkeys.com/pc/games?limit=50 有打折和没有打折的游戏。
allowed_urls = ['https://www.cdkeys.com/pc/games?limit=50?']
start_urls = ['https://www.cdkeys.com/pc/games/{pageno}?limit=50'.format(pageno=pageno)
for pageno in range(1, 10)]
def parse(self, response):
Games = response.xpath('//*[@id="root-wrapper"]/div/div[1]/div[2]/div[3]/div[2]/div[2]/ul/li/h2/a/text()').extract()
Prices = response.xpath('//span[starts-with(@id, "product-price-")]/span[1]/span/text()').extract()
for i, (Game, Price) in enumerate(zip(Games, Prices)):
yield {'index': i, 'Game': Game, 'Price':Price}
问题出在价格的 XPath 中,我可以得到只有折扣价的列表,或者只有没有折扣的游戏的价格列表,因为 HTML 对于这些类别来说是完全不同的。
阻止我简单地创建两个列表的原因是因为我使用的是 zip
和 enumerate
它只是迭代第一个 x
数量的游戏直到用完价格,而不是将每个游戏链接到相应的价格。
任何有关在 Prices
中仅显示正确价格或找到一种使用空值而不是替换以下值的方法的帮助将不胜感激。我对 python 和网络爬虫都是新手,只是想了解这一切。
我会采取不同的做法 - 逐一遍历产品项目,然后找到游戏名称、正常价格和折扣价:
def parse(self, response):
for game in response.css("ul.products-grid li.item"):
name = game.css("h2.product-name > a::text").extract_first()
old_price = game.css(".regular-price .price::text,.old-price .price::text").extract_first()
discount_price = game.css(".special-price .price::text").extract_first()
yield {
"name": name,
"old_price": old_price,
"discount_price": discount_price
}
对于第一页,您将获得以下输出:
{'old_price': u'$ 13.59', 'name': u'Stellaris: Utopia PC DLC', 'discount_price': None}
{'old_price': u' $ 9.49 ', 'name': u'Insurgency PC', 'discount_price': u' $ 1.99 '}
...
{'old_price': u' $ 81.59 ', 'name': u'Call of Duty Black Ops II 2 Digital Deluxe Edition PC ', 'discount_price': u' $ 13.59 '}
注意旧价格是如何填写有和没有折扣的。