【发布时间】:2021-02-25 22:06:18
【问题描述】:
我正在开发网络抓取工具,但在抓取正确的选择器时遇到了麻烦。这是我的代码:
# -*- coding: utf-8 -*-
import scrapy
import pandas as pd
from ..items import HomedepotpricespiderItem
from scrapy.http import Request
class HomedepotspiderSpider(scrapy.Spider):
name = 'homeDepotSpider'
allowed_domains = ['homedepot.com']
start_urls = ['https://www.homedepot.com/pep/304660691']#.format(omsID = omsID)
#for omsID in omsList]
def parse(self, response):
#call home depot function
for item in self.parseHomeDepot(response):
yield item
pass
def parseHomeDepot(self, response):
#get top level item
items = response.css('#zone-a-product')
for product in items:
item = HomedepotpricespiderItem()
#get the price
productPrice = product.xpath('//div[@class="price-format__main-price"]/span/text()').getall()
#get rid of all the stuff i dont need
item['productPrice'] = productPrice
yield item
因此,使用我当前的选择器,它看起来像是在获取这些项目的价格。
因为我的输出是:
'productPrice': ['$',
'2167',
'49',
'$',
'1798',
'00',
'$',
'2698',
'00',
'$',
'2099',
'99',
'$',
'2968',
'00',
'$',
'2294',
'99',
'$',
'2068',
'00',
'$',
'1649',
'99',
'$',
'2399',
'00',
'$',
'1649',
'99',
'$',
'1549',
'99',
'$',
'1799',
'99',
'$',
'3360',
'89',
'$',
'2899',
'95',
'$',
'3699',
'00',
'$',
'2719',
'96',
'$',
'1954',
'99',
'$',
'2699',
'00',
'$',
'2294',
'96',
'$',
'3149',
'00',
'$',
'3499',
'00',
'$',
'3749',
'00',
'$',
'4999',
'00',
'$',
'2799',
'99'],
什么时候正确的输出应该是:2099
此外,我认为我的选择器根本没有抢到商品的价格。
【问题讨论】:
标签: python html css web-scraping scrapy