Я совершенно новичок в Python и scrapy, и я пытаюсь передать xpath в качестве аргумента пауку через командную строку. Мне удается успешно передать его, но когда я добавляю его в метод response.xpath, вместо получения соответствующего ответа, он просто возвращает строку xpath, которую я передал через командную строку. Это прекрасно работает, если я просто передаю xpath через сам код, а не xpath.
Код моего паука:
name = "basic_scraper"
allowed_domains = ['some-site.com']
start_urls = [
'https://some-site.com/start/urls'
]
custom_settings = {'FEED_URI': "file-%(time)s.csv",
'FEED_FORMAT': 'csv'}
def parse(self, response):
#this is the xpath when passed through the cmd. Over here it just returns the xpath, instead of parsing it
urls = response.xpath(self.xpath_string).getall()
#This is the actual xpath, it works perfectly fine
#response.xpath('//div[@class="product__title text-center"]/a/@href').getall()
for url in urls:
print('I am supposed to be the url'+url)
yield scrapy.Request(self.allowed_domains[0]+url, callback=self.parse_urls, dont_filter=True)
next_page_url = response.xpath('//span[@class="next"]/a/@href').extract_first()
if next_page_url:
absolute_next_page_url = self.allowed_domains[0] + next_page_url
yield scrapy.Request(absolute_next_page_url, callback=self.parse, dont_filter=True)
# print(urls[0])
def parse_urls(self, response):
# get product_details
product_name = response.xpath('//div[@class="product-single__meta small--text-center"]/h1/text()').get()
product_price = response.xpath('//p[@class="product-single__prices"]/span[@class="product-single__price"]/text()').get()
product_description = response.xpath('//div[@class="product-description"]/h1/text()').get(default=None)
product_sku = response.xpath('//div[@class="product-sku"]/h1/text()').get(default=None)
product_image = response.xpath('//div[@class="product-single__photos"]/img/@src').get(default=None)
image = product_image.split('?')
# for item in data:
prod_data = {
'Category': 'Cat 1',
'Image_Url': 'https:'+image[0],
'Product_Description': '.' if product_description is None else product_description,
'Product_Name': product_name,
'Product_url': response.url,
'Selling_Price': product_price.replace('\n', ''),
'SKU': random.randrange(5000) if product_sku is None else product_sku,
}
yield prod_data
Это команда, которую я запускаю в командной строке: scrapy.exe crawl basic_scrapper -a xpath_string = '//div[@class="product__title text-center"]/a/@href'
Любая помощь будет принята с благодарностью!