Tôi đang tạo một ứng dụng web để loại bỏ một danh sách dài các loại giày từ các trang web khác nhau. Đây là hai tập lệnh vụn vặt riêng biệt của tôi:
http://store.nike.com/us/en_us/pw/mens-clearance-soccer-shoes/47Z7puZ896Zoi3
từ nhện nhập khẩu phế liệu
từ yêu cầu nhập Scrapy.http
lớp GiàySpider(Nhện):
tên = "giày"
allow_domains = ["store.nike.com"]
start_urls = ['http://store.nike.com/us/en_us/pw/mens-clearance-soccer-shoes/47Z7puZ896Zoi3']
phân tích def (tự, phản hồi):
giày = reply.xpath('//*[@class="grid-item-image-wrapper sprite-sheet sprite-index-0"]/a/@href').extract()
cho giày trong giày:
Yêu cầu năng suất(giày, gọi lại=self.parse_shoes)
def pars_shoes(tự, phản hồi):
url = phản hồi.url
name = reply.xpath('//*[@itemprop="name"]/text()').extract_first()
giá = reply.xpath('//*[@itemprop="price"]/text()').extract_first()
giá = price.replace('$','')
shoe_type = reply.css('.exp-product-subtitle::text').extract_first()
size = reply.xpath('//*[@class="nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown"]/option')
size = size.xpath('text()[not(parent::option/@class="exp-pdp-size-not-in-stock selectBox-disabled")]').extract()
kích thước = [s.strip() cho s ở kích thước]
năng suất {
'url': url,
'tên': tên,
'price': giá cả,
'sizes' : kích thước,
'loại giày': loại giày
}
http://www.dickssportinggoods.com/products/clearance-soccer-cleats.jsp
từ nhện nhập khẩu phế liệu
từ yêu cầu nhập Scrapy.http
lớp GiàySpider(Nhện):
tên = "giày"
allow_domains = ["dickssportinggoods.com"]
start_urls = ['http://www.dickssportinggoods.com/products/clearance-soccer-cleats.jsp']
phân tích def (tự, phản hồi):
giày = reply.xpath('//*[@class="fplpTitle header4"]/a/@href').extract()
cho giày trong giày:
Yêu cầu năng suất(giày, gọi lại=self.parse_shoes)
def pars_shoes(tự, phản hồi):
kích thước = reply.xpath('//*[@class="swatches clearfix"]/input/@value').extract()
nếu kích thước == []:
vượt qua
url = phản hồi.url
name = reply.xpath('.//*[@id="PageHeading_3074457345618261107"]/h1/text()').extract_first()
giá = reply.xpath('.//*[@itemprop="price"]/text()').extract_first()
#shoe_type = reply.css('.exp-product-subtitle::text').extract_first()
năng suất {
'url': url,
'tên': tên,
'price': giá cả,
'sizes' : kích thước,
'loại_giày': ''
}
Làm thế nào tôi có thể đặt chúng lại với nhau? Tôi đã xem qua tài liệu sơ sài nhưng tôi không thấy họ đề cập đến điều này, nó chỉ đề cập đến cách lấy cả hai địa chỉ từ địa chỉ gốc. Cảm ơn
Đặt hai tên miền của bạn vào allow_domains
, hãy đặt hai URL của bạn vào bắt đầu_url
, sau đó sử dụng if-else đơn giản để xác định phần nào của mã sẽ thực thi.
từ nhện nhập khẩu phế liệu
từ yêu cầu nhập Scrapy.http
lớp GiàySpider(Nhện):
tên = "giày"
allow_domains = ["store.nike.com", "dickssportinggoods.com"]
start_urls = ['http://store.nike.com/us/en_us/pw/mens-clearance-soccer-shoes/47Z7puZ896Zoi3', 'http://www.dickssportinggoods.com/products/clearance-soccer-cleats. jsp']
phân tích def (tự, phản hồi):
nếu "store.nike.com" trong reply.url:
giày = reply.xpath('//*[@class="grid-item-image-wrapper sprite-sheet sprite-index-0"]/a/@href').extract()
Elif "dickssportinggoods.com" trong reply.url:
giày = reply.xpath('//*[@class="fplpTitle header4"]/a/@href').extract()
cho giày trong giày:
Yêu cầu năng suất(giày, gọi lại=self.parse_shoes)
def pars_shoes(tự, phản hồi):
url = phản hồi.url
nếu "store.nike.com" trong reply.url:
name = reply.xpath('//*[@itemprop="name"]/text()').extract_first()
giá = reply.xpath('//*[@itemprop="price"]/text()').extract_first()
giá = price.replace('$','')
shoe_type = reply.css('.exp-product-subtitle::text').extract_first()
size = reply.xpath('//*[@class="nsg-form--drop-down exp-pdp-size-dropdown exp-pdp-dropdown two-column-dropdown"]/option')
size = size.xpath('text()[not(parent::option/@class="exp-pdp-size-not-in-stock selectBox-disabled")]').extract()
kích thước = [s.strip() cho s ở kích thước]
năng suất {
'url': url,
'tên': tên,
'price': giá cả,
'sizes' : kích thước,
'loại giày': loại giày
}
Elif "dickssportinggoods.com" trong reply.url:
kích thước = reply.xpath('//*[@class="swatches clearfix"]/input/@value').extract()
nếu kích thước == []:
vượt qua
url = phản hồi.url
name = reply.xpath('.//*[@id="PageHeading_3074457345618261107"]/h1/text()').extract_first()
giá = reply.xpath('.//*[@itemprop="price"]/text()').extract_first()
#shoe_type = reply.css('.exp-product-subtitle::text').extract_first()
năng suất {
'url': url,
'tên': tên,
'price': giá cả,
'sizes' : kích thước,
'loại_giày': ''
}
Tôi là một lập trình viên xuất sắc, rất giỏi!