import scrapy
from product.items import ProductItem
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class amazonSpider(scrapy.Spider):
name = "amazon"
allowed_domains = ["amazon.in"]
start_urls = [ main url here]
def parse(self, response):
item=ProductItem()
for content in response.xpath("sample xpath"):
url = content.xpath("a/@href").extract()
request = scrapy.Request(str(url[0]),callback=self.page2_parse)
#url is extracted from my main url
item['product_Rating'] = request
yield item
def page2_parse(self,response):
#here i dint get the response for the second url content
for content in response.xpath(sample xpath):
yield content.xpath(sample xpath).extract()
此处未执行第二项功能。请帮帮我。
答案 0 :(得分:0)
最后我已经这样做了,请按照以下代码实现url url的抓取值。
def parse(self, response):
item=ProductItem()
url_list = [content for content in response.xpath("//div[@class='listing']/div/a/@href").extract()]
item['product_DetailUrl'] = url_list
for url in url_list:
request = Request(str(url),callback=self.page2_parse)
request.meta['item'] = item
yield request
def page2_parse(self,response):
item=ProductItem()
item = response.meta['item']
item['product_ColorAvailability'] = [content for content in response.xpath("//div[@id='templateOption']//ul/li//img/@color").extract()]
yield item