【发布时间】:2021-05-20 16:50:56
【问题描述】:
我一直在尝试从该网站 (https://dentalspeed.com/?fbclid=IwAR1_gjjWAevu1pgikjwLUqeFXzjBRo7A93uXFSIAasxlvl97ptEorNP1fDo) 抓取 产品名称和价格,但不幸的是我无法正确获取 CSS 选择器。我还使用了 CSS 选择器小工具。我也知道 html 和 css,我自己也读过。我认为 css 选择器是正确的,但由于某种原因我无法提取数据。
def parse(self, response):
items = DenItem()
all_div = response.css('div.collection-product')
for div in all_div:
product_name = div.css(".collection-product-name font font::text").extract()
_new_price = div.css('div.collection-product-price > a > font > font::text').extract() # .replace("Rs", "")
_new_price = [s.replace("$", "") for s in _new_price]
_new_price = [s.replace(",", "") for s in _new_price]
_old_price = div.css("main#setembro section:nth-child(5) > div > div > div > div > ul > div.owl-wrapper-outer > div > div:nth-child(3) > li > div > div.collection-product-price-content > p.collection-product-price > del > font > font::text").extract() # .replace("Rs", "")
_old_price = [n.replace("R $", "") for n in _old_price]
_old_price = [n.replace(",", "") for n in _old_price]
items['product_name'] = product_name
items['_new_price'] = _new_price
items['_old_price'] = _old_price
if len(items['_new_price']) == 0:
items['_new_price'] = '0'
if len(items['_old_price']) == 0:
items['_old_price'] = '0'
yield items
【问题讨论】:
-
分享你目前拥有的代码,没有它你只是要求别人为你做。
-
我已经分享了
-
哪些产品?有很多。此外, div.collection-product 不会为我产生任何结果,因此您不会执行循环。
标签: python web-scraping